Partial classes, or Partial Types, in object oriented computer programming languages, means the ability to split a class' definition across several files, or several places within a single file. The family of Microsoft .NET languages (C#, VB.NET, etc.), as of their current incarnation (.NET 2.0) have come to support them.
file1.vb: Partial Class MyClass Private _name As String End Class
file2.vb: Partial Class MyClass Public Readonly Property Name() As String Get Return _name End Get End Property End Class
When compiled, the result is the same as the following code:
Class MyClass Private _name As String Public Readonly Property Name() As String Get Return _name End Get End Property End Class
Partial classes also support the separation of concerns, the following Bear class has different aspects implemented in different parts:
Bear_Hunting.cs
public partial class Bear
{
private IEdible Hunt()
{
// returns food...
}
}
Bear_Eating.cs
public partial class Bear
{
private int Eat(IEdible food)
{
return food.Nutrition.Value;
}
}
Bear_Hunger.cs
public partial class Bear
{
private int hunger;
public void MonitorHunger()
{
// Here we can refer to members of the other partial definitions
if(hunger > 50)
hunger -= this.Eat(this.Hunt());
}
}
For example, if we want to compile a version without support for hunger management (it could be a feature that costs extra), we simply remove the partial declaration in Bear_Hunger.cs.
Now if your program also supported hunger management in other classes you might want to put all those partial class definitions into a separate 'Hunger' directory. You'd then be doing what is usually called Multidimensional separation of concerns, and that would help you update the code and add new features, even if it was the first time you started working with this code.
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Partial classes".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world