Partial classes provide an ability to split class declaration (usually into separate files). A common problem that can be solved with partial classes is allowing users to modify auto-generated code without fearing that their changes will be overwritten if the code is regenerated. Also multiple developers can work on same class or methods.
using System;
namespace PartialClassAndMethods
{
public partial class PartialClass
{
public void ExampleMethod() {
Console.WriteLine("Method call from the first declaration.");
}
}
public partial class PartialClass
{
public void AnotherExampleMethod()
{
Console.WriteLine("Method call from the second declaration.");
}
}
class Program
{
static void Main(string[] args)
{
PartialClass partial = new PartialClass();
partial.ExampleMethod(); // outputs "Method call from the first declaration."
partial.AnotherExampleMethod(); // outputs "Method call from the second declaration."
}
}
}