< C Sharp Programming

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 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()
    {
    }
}
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.