What is the purpose of partial classes?

I read about partial classes and, for example, I understand why they are used when Visual Studio creates Windows Forms, not excluding that

when working on large projects, spreading the class to separate files allows you to simultaneously work with several programmers.

.

Later I read this example , and I noticed that most classes are declared as partial. Why?

+8
c #
source share
5 answers

Partial classes allow the code not only to add to the generated classes, but also to implement additional functions.

For example, code can be generated that performs a property check (via IDataErrorInfo), but a check for each property cannot be determined at the time of generation, a partial class allows the developer to specify a check at a later point.

Generated File:

public partial class MyClass : IDataErrorInfo { partial void ValidateMyProperty(ref string error); public string this[string propertyName] { get { string error = String.Empty; if (propertyName == "MyProperty") ValidateMyProperty(ref error); return error; } } public string Error { get { return String.Empty; } } public int MyProperty { get; set; } } 

Designed file:

 public partial class MyClass { partial void ValidateMyProperty(ref string error) { if (MyProperty < 0) error = "MyProperty cannot be negative."; } } 
+5
source share

One of the very good examples when useful classes are useful is when a class is automatically generated by a tool (for example, VS), and you want to expand the capabilities of this class and not lose your code when the tool needs to restore its code. For example, when you use the Visual Studio Entity Data Model Designer, entities (classes) will be generated as partial.

+11
source share

If you have a large class, it may be useful to split the definition between multiple source files.

In addition, if you want to extend the class that is generated — say, from a T4 or ORM template — or in any situation where you cannot or should not modify the original source file, you can use the additional definition of the partial class to add new functions that will not be deleted when the template or ORM restores the original class.

Edit

Just an observation, but your quote:

when working on large projects, spreading the class to separate files allows you to work simultaneously with several programmers.

Not very good with me, even though I came from MSDN. If you use a decent version control system (for example, TFS or even svn), merging in the source files, where each developer works on individual methods, handles pretty well. Saying that using partial classes simplifies the work of several developers on the same class (and probably I will bet), encouraged someone there to separate each method and property of each class in their decision into separate source files. This is bad. Really bad. As in case of need - to open-forward-forward-when-negotiations-salaries-with-new-hires badly.

+5
source share

One practical application is that you can modify a partial class in several places within the namespace area. For readability, you can split the class into several files.

IE:
In HelloWorld.cs in the "Bleh" namespace

 public partial class testClass { private int intMember; public string stringMember; } 

In HelloWorld2.cs in the namespace "Bleh"

 public partial class testClass { private int intAnotherInt; public string stringAnotherString; } 

Both declared classes are the same thing in reality, but the goal was to split the class into files. Both combined look like this:

 public partial class testClass { private int intAnotherInt; // Our int from HelloWorld2.cs public string stringAnotherString; // Our string from HelloWorld2.cs private int intMember; // Our int from HelloWorld.cs public string stringMember; // Our string from HelloWorld.cs } 

More info here .

+1
source share

since the link you provided means that you can have one class divided into several files and it will be recombined into one class at compile time.

as for the other link you posted ... partial classes are marked this way because the markup (aspx) has a class created by the constructor that gives you access to the things defined in the markup that I consider ...

therefore there are three files ... productlist.aspx, productlist.cs and productlist.designer.cs

productlist.aspx is the markup

productlist.cs is a partial class that the markup file refers to and contains your code.

productlist.designer.cs is a generated partial class that is merged with the .cs file above

0
source share

All Articles