Introduction
Partial definitions have been around for quite some time. A common scenario for partial classes is to separate the class code to two different cs
files. For example, a Windows Forms form where Visual Studio generates separately the Designer.cs file to contain the generated code.
However, the partial classes are not restricted only to two separate definitions. Actually there can be multiple partial definitions for a single class.
An example
A simple example could be a data class, where the following parts are distinguished:
- Data properties
- Formatted data representation
- Validations
- Operations
The data portion could look like the following:
public partial class SomeClass {
public string Text { get; set; }
public int? Number { get; set; }
}
So few simple properties to hold the data. Now the formatting could be separated to a different definition.
public partial class SomeClass {
public string FormattedText {
get {
string returnValue = "";
if (!string.IsNullOrEmpty(this.Text)) {
returnValue = this.Text.Trim().ToLower();
}
return returnValue;
}
}
public string FormattedNumber {
get {
string returnValue = "";
if (this.Number != null) {
returnValue = ((int)this.Number).ToString("C2");
}
return returnValue;
}
}
}
The string is trimmed and represented as lower case while the number is show as a currency string. The validations could look like
public partial class SomeClass {
public bool Validate() {
bool ok = true;
ok &= this.CheckText();
ok &= this.CheckNumber();
return ok;
}
private bool CheckText() {
bool ok = true;
if (this.Number != null) {
ok = !string.IsNullOrEmpty(this.Text);
}
return ok;
}
private bool CheckNumber() {
bool ok = true;
if (this.Number != null) {
ok = this.Number >= 0 && this.Number <= 50;
}
return ok;
}
}
The validations check that the number is between 0 and 50 and if the number is defined (it's not null) the text is mandatory. And lastly the operations
public partial class SomeClass {
public void WriteToConsole() {
if (!this.Validate()) {
System.Console.WriteLine("Validaton failed");
return;
}
System.Console.WriteLine(string.Format("Text is {0}", this.FormattedText));
System.Console.WriteLine(string.Format("Number is {0}", this.FormattedNumber));
}
}
This portion has a single method to write the data to the console if the validation succeeds.
So now we have a single class definition, which is split into four separate cs
files. Calling the class could look like this
class Program {
static void Main(string[] args) {
SomeClass instance = new SomeClass();
instance.Number = 100;
instance.Text = "1st definition";
instance.WriteToConsole();
instance.Number = 45;
instance.Text = "2nd definition";
instance.WriteToConsole();
}
}
So when to use
There's no single guideline when to use multiple partial definitions. Few possibilities could be:
- Several parts of the class are generated and the code generation is done separately from each other
- When the code has to be split, for example based on the usage and
#region
directive isn't satisfactory - Different people work on different parts of a class simultaneously (possibly in a version controlled environment)
The attached download contains the example project for this class.
History
- November 17, 2012: Tip created.