Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are partial methods used in C# 3.0?

I have read about partial methods in the latest C# language specification, so I understand the principles, but I'm wondering how people are actually using them. Is there a particular design pattern that benefits from partial methods?

like image 767
Eric Z Beard Avatar asked Sep 03 '08 17:09

Eric Z Beard


2 Answers

Partial methods have been introduced for similar reasons to why partial classes were in .Net 2.

A partial class is one that can be split across multiple files - the compiler builds them all into one file as it runs.

The advantage for this is that Visual Studio can provide a graphical designer for part of the class while coders work on the other.

The most common example is the Form designer. Developers don't want to be positioning buttons, input boxes, etc by hand most of the time.

  • In .Net 1 it was auto-generated code in a #region block
  • In .Net 2 these became separate designer classes - the form is still one class, it's just split into one file edited by the developers and one by the form designer

This makes maintaining both much easier. Merges are simpler and there's less risk of the VS form designer accidentally undoing coders' manual changes.

In .Net 3.5 Linq has been introduced. Linq has a DBML designer for building your data structures, and that generates auto-code.

The extra bit here is that code needed to provide methods that developers might want to fill in.

As developers will extend these classes (with extra partial files) they couldn't use abstract methods here.

The other issue is that most of the time these methods wont be called, and calling empty methods is a waste of time.

Empty methods are not optimised out.

So Linq generates empty partial methods. If you don't create your own partial to complete them the C# compiler will just optimise them out.

So that it can do this partial methods always return void.

If you create a new Linq DBML file it will auto-generate a partial class, something like

[System.Data.Linq.Mapping.DatabaseAttribute(Name="MyDB")]
public partial class MyDataContext : System.Data.Linq.DataContext
{
    ...

    partial void OnCreated();
    partial void InsertMyTable(MyTable instance);
    partial void UpdateMyTable(MyTable instance);
    partial void DeleteMyTable(MyTable instance);

    ...

Then in your own partial file you can extend this:

public partial class MyDataContext
{
    partial void OnCreated() {
        //do something on data context creation
    }
}

If you don't extend these methods they get optimised right out.

Partial methods can't be public - as then they'd have to be there for other classes to call. If you write your own code generators I can see them being useful, but otherwise they're only really useful for the VS designer.

The example I mentioned before is one possibility:

//this code will get optimised out if no body is implemented
partial void DoSomethingIfCompFlag();

#if COMPILER_FLAG
//this code won't exist if the flag is off
partial void DoSomethingIfCompFlag() {
    //your code
}
#endif

Another potential use is if you had a large and complex class spilt across multiple files you might want partial references in the calling file. However I think in that case you should consider simplifying the class first.

like image 63
Keith Avatar answered Oct 12 '22 20:10

Keith


Partial methods are very similar in concept to the GoF Template Method behavioural pattern (Design Patterns, p325).

They allow the behaviour of an algorithm or operation to be defined in one place and implemented or changed elsewhere enabling extensibility and customisation. I've started to use partial methods in C# 3.0 instead of template methods because the I think the code is cleaner.

One nice feature is that unimplemented partial methods incur no runtime overhead as they're compiled away.

like image 21
Jonathan Webb Avatar answered Oct 12 '22 19:10

Jonathan Webb