Jump to content

C# Programming/Partial classes

From Wikibooks, open books for an open world

Partial Classes

[edit | edit source]

As the name indicates, partial class definitions can be split up across multiple physical files. To the compiler, this does not make a difference, as all the fragments of the partial class are grouped and the compiler treats it as a single class. One common usage of partial classes is the separation of automatically-generated code from programmer-written code.

Below is an example of a partial class.

Listing 1: Entire class definition in one file (file1.cs)

public class Node
{
    public bool Delete()
    {
    }

    public bool Create()
    {
    }
}

Listing 2: Class split across multiple files

(file1.cs)

public partial class Node
{
    public bool Delete()
    {
    }
}

(file2.cs)

public partial class Node
{
    public bool Create()
    {
    }
}