473,387 Members | 1,590 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,387 software developers and data experts.

Partial Methods and C# 3.0

I was reading about partial methods in the upcoming C# 3.0 with a
friends and we were having a discussion about their usefulness.

One on hand, they seem to be ok at providing a solution for allowing a
class to be extended at compile time by users of the class, and as
pointed out in some articles and papers they are good at acting as
lightweight event handlers.

On the other hand, they are in my eyes exactly almost a strict subset
of the delegate/event model already provided by C# and the .NET
framework.

The idea is that you can do something like this:

partial class Foo
{
partial void SomeEventHandler(int i);

int VeryExpensiveMethod() { ... }

public void Method()
{
SomeEventHandler(VeryExpensiveMethod());
}
}

Now, this code would exist in a library somewhere and a user of the
library would simply type the following code:

partial class Foo
{
partial void SomeEventHandler(int i) {/* Do something with i */}
}

If that code is not present, then Foo.Method() would essentially
compile down to an completely empty method. The advantage over using
the standard Strategy design pattern (e.g. virtual methods) is that
VeryExpensiveMethod() will be compiled out since all this information
is known at compile time.

But the advantage over using the delegate/event model is minimal, and
almost non-existant. Consider if the class had been written like
this:

class Foo
{
public void delegate SomeEventHandler(int i);
public event SomeEventHandler OnSomeEvent;

int VeryExpensiveMethod() { ... }

public void Method()
{
if (OnSomeEvent != null)
OnSomeEvent(VeryExpensiveMethod());
}
}

This is almost equivalent, the only obvious difference being that in
the case of the delegate/event, OnSomeEvent will always be checked for
null, and the method will never compile down to an empty method.
But... Have they really designed an entire language feature and
keyword usage around one micro-optimization? The latter is actually
more maintainable and clearer IMO, and definitely more flexible. The
only price is that you have to suffer a single null pointer
comparison, as well as any overhead incurred by the Delegate or
MulticastDelegate classes for the actual dispatch.
Am I missing something here? Is there some elegant design pattern
that this can be used with? Partial -classes- actually solved a real
problem, in particular that generated classes could not be customized
in such a way that re-generating the code would not interfere with the
customizations.

But partial methods seem to solve nothing unless you're writing
extremely performance intensive code, in which case I would argue that
maybe you shouldn't be using C# in the first place.

Thoughts?
Nov 20 '07 #1
1 1244
Forgetting best-practice about listening to your own events (or
rather: not), how (when?) would you ever subscribe to your own events
if the constructor you are interested in is in the generated part of
the class? You can't override your own methods, for instance.

Also - in what way more maintainable? You've had to introduce a lot of
extra fluff (a delegate declaration, an event declaration, an event
reference in every instance [ok, you could use EventHandlerList here],
the null-check, etc). And of course some ctor mangling to subscribe.
Now (taking sqlmetal classes as an example) multiply this by twice the
property count (both in the setter, one pre, one post) plus a few
extras. That is a *lot* of maintenance.

The partial method is by comparison and IMO much cleaner. And when you
don't implement something it simply evaporates; no effort.

Marc
Nov 20 '07 #2

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

Similar topics

7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
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. ...
3
by: The Crow | last post by:
for example can we write a partial public class String {} and add some static/instance methods to String class of .Net 2.0 framework?
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...
9
by: Fat Elvis | last post by:
I'd like to extend some of my Asp.net pages by using Partial Classes. Example ASP.Net Page: public partial class Admin_Customer : System.Web.UI.Page { protected void Page_Load(object sender,...
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...
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...
10
by: JDeats | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.