Skip to main content

C# - Partial Types

What is Partial Types ?

Partial types definition allows for the definition of class, struct or interface to be split into multiple files.
e.g:
public partial class Employee
{
    public void DoWork()
    {
    }
}

public partial class Employee
{
    public void GoToLunch()
    {
    }
}

In which version it was introduced.
It introduced in C# 2.0

What are the benefits of using Partial Types ?
- In large projects, it helps multiple programmers to work on the same class at the same time.
- In auto-generated scripts, it helps to generate only new changes in new partial class instead of creating the whole class.

What are the limitations/rules of using Partial Types ?
- All partial classes must be defined in the same assembly or module.
- The class name and generic type parameters must match in all partial type definition.


The following are merged from all the partial-type definitions:
XML comments
interfaces
generic-type parameter attributes
class attributes
members

For example, consider the following declarations:

partial class Earth : Planet, IRotate { }
partial class Earth : IRevolve { }

They are equivalent to the following declarations:

class Earth : Planet, IRotate, IRevolve { }