简体   繁体   中英

Practical usage of partial keyword in C#

I know it lets visual studio to separate WinForms UI code from the UI events, etc. But are there any practical uses for it?

The partial keyword is typically used in code generation utilities to allow developers to add additional functionality to the generated code without the fear of that code being erased if the code is generated again.

With C# 3 the partial keyword can be applied to methods to allow users of generated code to fill in blanks left by the generator. The Linq To Sql designer for example provides partial methods that allow you to add logic to the classes that the framework will call if implemented. The benefit here is that the C# compiler will completely remove unimplemented partial methods, so there is no performance hit at all for not implementing them.

Partial classes can also be used to organize very large classes into separate code files, although this sort of usage is usually a sign that your classes are too large and are taking on too many responsibilities.

The best use for the partial keyword that I can think of is nested classes. Without the partial keyword all nested classes must go in the same code file as the containing class.

One use I have found is for code that you may not want to exist in production code such as tracing or dev logging code. You could place this in the partial class and then when you do a production build you simply build without those partials included and the compiler will auto-magically strip out any calls to those partials that were excluded.

I'm using it for partitioning helper classes where some methods need lots of code. This is quite an easy way of retaining readability as each file only deals with a portion of the class's functionality.

In VB you can use it to separate "normal" code from code that needs late binding via "Option Strict Off".

In C# I only use it for unusually large classes.

The LINQ to SQL designer uses it extensively to split custom behaviors (partial methods) outside of the mapping class.
In short, its main purpose is for code generators.
Hope this helps.

I use partial classes and methods all the time with Linq2Sql. Otherwise I have never used it for anything besides UI/Code-behind classes.

One of the great things that I like about partial classes in linq2sql is that you can have the partial class inherit an interface and it will map the interface to the generated class. This is great if you have more than one Data provider and still want to use the same interfaces for all the Data access classes.

WPF uses partial classes extensively. The XAML generates a partial class that you can add to.

I use partial classes to reduce the merge pain with version control when I know that we would have several developers working on one class. For example, we often have our DAL class split into several partial class files. If you don't put them in different files, it's easy to get merge conflicts that take a while to fix when checked into version ctl.

When the develoment gets less chaotic as the class gets close to complete, we get rid of the partial files. We just use it to make VC/mulit-developer issues easier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM