How are partial methods used in C# 3.0?

╄→尐↘猪︶ㄣ 提交于 2020-01-10 12:37:09

问题


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?


回答1:


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.




回答2:


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.




回答3:


Code generation is one of main reasons they exist and one of the main reasons to use them.


EDIT: Even though that link is to information specific to Visual Basic, the same basic principles are relevant to C#.




回答4:


I see them as lightweight events. You can have a reusable code file (usually autogenerated but not necessarily) and for each implementation, just handle the events you care about in your partial class. In fact, this is how it's used in LINQ to SQL (and why the language feature was invented).




回答5:


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 (with the exception of partials created by Visual Studio for designer files; those are great). To me, it's more important to have all of the code for a class in one place. If your class is well designed and represents one thing (single responsibility principle), then all of the code for that one thing should be in one place.



来源:https://stackoverflow.com/questions/42187/how-are-partial-methods-used-in-c-sharp-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!