C Sharp Programming/Partial classes

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

[edit] Partial Classes

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 the 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()
    {
    }
}