473,395 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Looking for other C# programmers thoughts on partial methods

So I have a class that spans over two partial classes in code, here's
an example (do not read much into this, the code is of no practical
use, this is just a simple example of where my confusion occurs).

// Inside SharedClassExample1.cs
public partial class SharedClassExample
{

public List<stringBooksOnShelf { get; set; }
public List<stringBooksOnDesk { get; set; }

// constructor
public SharedClassExample()
{
BooksOnShelf = new List<string>();
BooksOnDesk = new List<string>();
}

public void SortBooksOnShelf()
{
BooksOnShelf.Sort();
}

// why isn't this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnShef.Remove(bookTitle);
}
}

// inside SharedClassExample2.cs
public partial class SharedClassExample
{
public void SortBooksOnDesk()
{
BooksOnDesk.Sort();
}

// why isn't this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnDesk.Remove(bookTitle);
}
}
The above example generates a compile time error, after some research
on-line it appears partial methods do not work as I original
understood. Instead a "partial method" is simply defined in one file
(In context, "file" means one of the two files in which this class
lives, in two parts) and the implementation is provided in the other
file.

Some my question is this: If the implementation of the partial method
can not span across files, then what is the point? For example, in C#
2.0 with partial classes I could make a call SortBooksOnDesk from
inside SharedClassExample1.cs so why do I need this new mechanism of
seperating the implementation from the declaration?


Aug 6 '08 #1
10 1498
JDeats wrote:
Some my question is this: If the implementation of the partial method
can not span across files, then what is the point?
The point is to allow code generators to generate code that can still be
extended by clients, without having to modify the generated code:

partial class Generated {
private void DoStuff() {
// Generated code here.
AfterDoStuff();
}

partial private void AfterDoStuff();
}

The client can now write up the other half of the class, implementing
AfterDoStuff(). If they don't, then there is no method and the call is
simply eliminated.

This could have been implemented with delegates or even events as well, but
if you have a lot of hooks this way (such as the ones provided by
LINQ-generated DataContext classes) this gets pretty clusmy and inefficient.
Enter partial methods as a simple, lightweight extension mechanism.

Basically, code generation is all that partial methods (and partial classes)
are intended to support.
For example, in C# 2.0 with partial classes I could make a call
SortBooksOnDesk from inside SharedClassExample1.cs so why do I need this
new mechanism of seperating the implementation from the declaration?
*You* don't. In fact, unless you're writing a code generator, there's no
reason to bother with "partial" at all. It only makes things harder to find.
If your class is so big that "partial" starts to look nice, it's time to
rewrite the class. Either that or paper over the hard bits with code
regions. That's still better than splitting up the class over multiple files.

--
J.
Aug 6 '08 #2
On Aug 6, 2:39 pm, JDeats <Jeremy.De...@gmail.comwrote:
So I have a class that spans over two partial classes in code, here's
an example (do not read much into this, the code is of no practical
use, this is just a simple example of where my confusion occurs).

// Inside SharedClassExample1.cs
public partial class SharedClassExample
{

public List<stringBooksOnShelf { get; set; }
public List<stringBooksOnDesk { get; set; }

// constructor
public SharedClassExample()
{
BooksOnShelf = new List<string>();
BooksOnDesk = new List<string>();
}

public void SortBooksOnShelf()
{
BooksOnShelf.Sort();
}

// why isn't this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnShef.Remove(bookTitle);
}

}

// inside SharedClassExample2.cs
public partial class SharedClassExample
{
public void SortBooksOnDesk()
{
BooksOnDesk.Sort();
}

// why isn't this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnDesk.Remove(bookTitle);
}

}

The above example generates a compile time error, after some research
on-line it appears partial methods do not work as I original
understood. Instead a "partial method" is simply defined in one file
(In context, "file" means one of the two files in which this class
lives, in two parts) and the implementation is provided in the other
file.

Some my question is this: If the implementation of the partial method
can not span across files, then what is the point? For example, in C#
2.0 with partial classes I could make a call SortBooksOnDesk from
inside SharedClassExample1.cs so why do I need this new mechanism of
seperating the implementation from the declaration?
IT's different than partial classes, in partial classes you are
effectively dividing an entity (in this case a class) among more than
one file.
partial methods are intended to be like more like a "if exist use it
if not ignore the call" kind of approach.
IIRC the iroginal intention was for the code generations framework
(like LINQ to SQL) where you can generate methods that call other
methods that might be implemented by the user, if those method exist
then the call is generated, if not the call is not generate hence no
performance overhead occur in the compiled code
Aug 6 '08 #3
On Aug 6, 1:56 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On Aug 6, 2:39 pm, JDeats <Jeremy.De...@gmail.comwrote:
So I have a class that spans over two partial classes in code, here's
an example (do not read much into this, the code is of no practical
use, this is just a simple example of where my confusion occurs).
// Inside SharedClassExample1.cs
public partial class SharedClassExample
{
public List<stringBooksOnShelf { get; set; }
public List<stringBooksOnDesk { get; set; }
// constructor
public SharedClassExample()
{
BooksOnShelf = new List<string>();
BooksOnDesk = new List<string>();
}
public void SortBooksOnShelf()
{
BooksOnShelf.Sort();
}
// why isn't this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnShef.Remove(bookTitle);
}
}
// inside SharedClassExample2.cs
public partial class SharedClassExample
{
public void SortBooksOnDesk()
{
BooksOnDesk.Sort();
}
// why isn't this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnDesk.Remove(bookTitle);
}
}
The above example generates a compile time error, after some research
on-line it appears partial methods do not work as I original
understood. Instead a "partial method" is simply defined in one file
(In context, "file" means one of the two files in which this class
lives, in two parts) and the implementation is provided in the other
file.
Some my question is this: If the implementation of the partial method
can not span across files, then what is the point? For example, in C#
2.0 with partial classes I could make a call SortBooksOnDesk from
inside SharedClassExample1.cs so why do I need this new mechanism of
seperating the implementation from the declaration?

IT's different than partial classes, in partial classes you are
effectively dividing an entity (in this case a class) among more than
one file.
partial methods are intended to be like more like a "if exist use it
if not ignore the call" kind of approach.
IIRC the iroginal intention was for the code generations framework
(like LINQ to SQL) where you can generate methods that call other
methods that might be implemented by the user, if those method exist
then the call is generated, if not the call is not generate hence no
performance overhead occur in the compiled code
Thanks! You have both helped clear this up. Based on my new
understanding I think the term "partial class" is a misnomer, but
other than that I'm perfectly fine with them :)
Aug 6 '08 #4
Correction: I think the term "partial method" is a misnomer. Wish the
team would have used a different term to describe this functionality.

Aug 6 '08 #5
JDeats wrote:
Correction: I think the term "partial method" is a misnomer. Wish the
team would have used a different term to describe this functionality.
What, introduce a *new keyword*? That's a mortal sin among language
designers if you've got an existing one you can co-opt. Admittedly, C++ took
this a little too far... :-)

--
J.
Aug 6 '08 #6
On Aug 6, 3:04 pm, JDeats <Jeremy.De...@gmail.comwrote:
Correction: I think the term "partial method" is a misnomer. Wish the
team would have used a different term to describe this functionality.
well, they could have choose a better one, that's for sure :)
Aug 6 '08 #7
On Aug 7, 4:55*am, Jeroen Mostert <jmost...@xs4all.nlwrote:
JDeats wrote:
rewrite the class. Either that or paper over the hard bits with code
regions. That's still better than splitting up the class over multiple files.

--
J.
Except where different areas of the code might need to be worked on by
different developers - in which case it can be quite useful.
Aug 7 '08 #8
..\\axxx wrote:
On Aug 7, 4:55 am, Jeroen Mostert <jmost...@xs4all.nlwrote:
>JDeats wrote:
>rewrite the class. Either that or paper over the hard bits with code
regions. That's still better than splitting up the class over multiple files.
Except where different areas of the code might need to be worked on by
different developers - in which case it can be quite useful.
Two thoughts:

1. Source control with merge semantics. It's what's for dinner.
2. If you can meaningfully split up a class so that two developers can work
on it independently, then it's two classes and glue, or even just two
classes. Refactoring time.

To put it clearer: I don't doubt that you *can* use "partial" to split up a
class to achieve something useful. I'm just saying that (in my rather
quickly formed opinion) you shouldn't, as there are clearly better
alternatives to achieve the same thing. The one clear exception is what the
thing was designed for: combining (re)generated code with manually written code.

--
J.
Aug 7 '08 #9
On Aug 6, 7:46 pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
.\\axxx wrote:
On Aug 7, 4:55 am, Jeroen Mostert <jmost...@xs4all.nlwrote:
JDeats wrote:
rewrite the class. Either that or paper over the hard bits with code
regions. That's still better than splitting up the class over multiple files.
Except where different areas of the code might need to be worked on by
different developers - in which case it can be quite useful.

Two thoughts:

1. Source control with merge semantics. It's what's for dinner.
2. If you can meaningfully split up a class so that two developers can work
on it independently, then it's two classes and glue, or even just two
classes. Refactoring time.

To put it clearer: I don't doubt that you *can* use "partial" to split up a
class to achieve something useful. I'm just saying that (in my rather
quickly formed opinion) you shouldn't, as there are clearly better
alternatives to achieve the same thing. The one clear exception is what the
thing was designed for: combining (re)generated code with manually written code.

--
J.
It seems these additions to the language were driven by the Visual
Studio,NET team to aid code generation. There are quite a few third-
party tools on the market that perform code generation that could make
use of these techniques, but they aren't for everyday use in my case.

Aug 7 '08 #10
On Aug 6, 7:46*pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
Two thoughts:

1. Source control with merge semantics. It's what's for dinner.
2. If you can meaningfully split up a class so that two developers can work
on it independently, then it's two classes and glue, or even just two
classes. Refactoring time.

To put it clearer: I don't doubt that you *can* use "partial" to split upa
class to achieve something useful. I'm just saying that (in my rather
quickly formed opinion) you shouldn't, as there are clearly better
alternatives to achieve the same thing. The one clear exception is what the
thing was designed for: combining (re)generated code with manually written code.
I think a good use, other than code generators, is to locate larger
nested classes into separate files.
Aug 7 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: tnrABC | last post by:
The approximately 100 books below are for sale. Mostly a selection of mathematics (numerical analysis mostly), computing science (graphics, ai, programming techniques, theory, compilers, operating...
14
by: Jason Daly | last post by:
I'm a freshman at college as a computer science major. I'm not sure it has what I want. Does anyone know if a major commonly exists in web design (focusing in server side languages)? I want to...
13
by: Gabriel Reid | last post by:
Greetings, I'm looking for any books that the members of this group may have to recommend for C#. I have experience with a number of programming languages (probably most relevant would have to...
9
by: Gomaw Beoyr | last post by:
Two question about the "partial classes" (in the next wersion of ..NET). Question 1 ========== Will partial classes (in the next version of C#) have to be declared "partial" in ALL places. ...
10
by: ptass | last post by:
Hi In asp.net 2.0 an aspx files .cs file is a partial class and all works fine, however, I thought I’d be able to create another class file, call it a partial class and have that compile and...
0
by: Dr. Peer Griebel | last post by:
I'm currently writing a small toy application to support symbolic algebra. Therefore I implemented some classes Term, Var, Number, Sum, Product, Power. These classes are tightly coupled. So it...
14
by: Rex | last post by:
Re: Looking for Tips/Writeup on overall approach to Exception Processing Hi All - I am fairly new to C# and am wondering how to best implement (overall) Exception Processing within my...
0
by: Atul Thombre | last post by:
Hello, I am developing a custom membership provider. For that I built a prototype that uses a SQL Server 2005 database as a backend store. I implemented the class...
3
by: Andrus | last post by:
I need to add methods without subclassing to run time compiled assembly at design time so that reflection also works . I need to define single type methods in two assemblies. I created...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.