Reputation: 8320
I read about partial classes and, for example, I understand the reason for they are used when Visual Studio creates Windows Forms, without ruling out that
when working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time
.
Later I read this example and I noticed that most of the classes are declared as partial. Why?
Upvotes: 13
Views: 5467
Reputation: 385
One practical application is that you can modify the partial class in more than one location, inside of the scope of the namespace. You could have a class split up into multiple files for the sake of readability.
IE:
In HelloWorld.cs in the namespace "Bleh"
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 classes being declared represent the same thing in reality, but the purpose was to split up the class into files. Both combined actually look like:
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 information here.
Upvotes: 3
Reputation: 29081
If you have a large class, it can be helpful to split the definition among multiple source files.
Also, if you want to extend a class that is generated - say, from a T4 template or by an ORM - or in any situation where you can't or shouldn't modify the original source file, you can use a additional partial class definition to add new functionality that won't be deleted when the template or ORM regenerate the original class.
Edit
Just an observation, but your quote:
when working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time
Doesn't sit very well with me, despite coming from MSDN. If you're using a decent source control system (like TFS or even svn), merges within source files where each dev is working on separate methods are handled pretty well. Saying that using partial classes makes it easier for multiple developers to work on the same class might (and, I'll wager, probably has) encouraged someone out there to split every method and property in every class in their solution into separate source files. That's bad. Really bad. As in, should-be-disclosed-up-front-when-negotiating-salaries-with-new-hires bad.
Upvotes: 5
Reputation: 19426
Partial classes allow code to not only add to the generated classes, but also implement optional functionality.
For example, there may be generated code which performs property validation (through IDataErrorInfo) but the validation on each property cannot be determined at generation time, the partial class allows the developer to specify the validation at a later point.
The 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; }
}
The developer implemented file:
public partial class MyClass
{
partial void ValidateMyProperty(ref string error)
{
if (MyProperty < 0)
error = "MyProperty cannot be negative.";
}
}
Upvotes: 7
Reputation: 3279
One of very good examples where partial classes come useful is when the class is auto generated by a tool (like VS) and you want to be able to extend functionality of that class and do not lose your code when the tool needs to regenerate its code. For example when you use Visual Studio Entity Data Model Designer the entities (classes) will be generated as partial.
Upvotes: 18
Reputation: 6199
as the link you posted mentioned it means you can have a single class split over multiple files and it will be recombined into a single class at compile time.
as for the other link you posted...the partial classes are marked that way because the markup (aspx) has a designer generated class which gives you access to the things defined in the markup I believe...
so, there are three files...productlist.aspx, productlist.cs, and productlist.designer.cs
productlist.aspx is the markup
productlist.cs is the partial class which is referenced from the markup file and contains your code.
productlist.designer.cs is the generated partial class which is combined with the .cs file above
Upvotes: 0