What are the partial methods used in C # 3.0?

I read about partial methods in the latest C # language specification , so I understand the principles, but I wonder how people actually use them. Is there a specific design pattern that uses partial methods?

+17
c # design-patterns partial-methods
Sep 03 '08 at 17:42
source share
5 answers

Partial methods were introduced for the same reasons as .Net 2 partial classes.

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

The advantage of this is that Visual Studio can provide a graphical constructor for part of the class, while encoders work on another.

The most common example is a form constructor. Developers most often do not want to position buttons, input fields, etc. Manually.

  • In .Net 1, it was generated by the generated code in the #region block
  • In .Net 2 they became separate classes of designers - the form is still one class, it just breaks into one file edited by the developers, and one form developer

This simplifies maintenance. Merging is simpler and less risky of the appearance of a VS-form constructor that accidentally violates manual changes to encoders.

In .Net 3.5 Linq introduced. Linq has a DBML constructor to create your data structures and generates autocode.

The extra bit here is the code needed to provide methods that developers might want to populate.

Since developers will extend these classes (with additional partial files), they cannot use abstract methods here.

Another problem is that most of the time these methods are not called, and calling empty methods is a waste of time.

Empty methods are not optimized .

So Linq generates empty partial methods. Unless you create your own partial to complete them, the C # compiler simply optimizes them.

So that these partial methods always return void.

If you create a new Linq DBML file, it will automatically 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 expand this:

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

If you do not extend these methods, they will be optimized correctly.

Partial methods cannot be publicly available - since then they must be there for other classes to call. If you write your own code generators, I see that they are useful, but otherwise they are really only useful for the VS constructor.

The example mentioned above is one of the possibilities:

 //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 spilled over multiple files, you may need partial links in the calling file. However, I think that in this case you should first consider class simplification.

+23
Sep 04 '08 at 11:52
source share

Partial methods are very similar in concept to the GoF Template Method behavior pattern ( Design Patterns , page 325).

They allow you to determine the behavior of an algorithm or operation in one place and be implemented or changed in another place, providing the ability to expand and configure. I started using partial methods in C # 3.0 instead of template methods because I think the code is cleaner.

One nice feature is that unrealized partial methods have no run-time overhead because they are compiled.

+10
Sep 03 '08 at 18:14
source share

Code generation is one of the main reasons they exist, and one of the main reasons for using them.




EDIT: Although this link refers to information specific to Visual Basic, the same basic principles apply to C #.

+2
Sep 03 '08 at 17:43
source share

I see them as light events. You may have a reusable file (usually auto-generated, but not necessary), and for each implementation, just handle the events that interest you in your partial class. In fact, this is how it is used in LINQ to SQL (and why the language was invented).

+2
Sep 03 '08 at 18:01
source share

Here is the best resource for partial classes in C # .NET 3.0: http://msdn.microsoft.com/en-us/library/wa80x488(VS.85).aspx

I try to avoid using partial classes (except for the partial ones created by Visual Studio for designer files, and this is great). It’s more important for me to have all the code for the class in one place. If your class is well designed and represents one thing (the principle of one responsibility ), then all the code for this should be in one place.

0
03 Sep '08 at 19:20
source share



All Articles