C# Partial Class and Partial Method

There are many situations when you might need to split a class definition, such as when working on a large scale projects, multiple developers and programmers might need to work on the same class at the same time. In this case we can use a feature called Partial Class.


Introduction to Partial Class

While programming in C# (or OOP), we can split the definition of a class over two or more source files. The source files contains a section of the definition of class, and all parts are combined when the application is compiled. For splitting a class definition, we need to use the partial keyword.

Example 1:

We have a project named as HeightWeightInfo which shows height and weight.

We have a file named as File1.cs with a partial class named as Record. It has two integer variables h & w and a method/constructor named as Record which is assigning the values of h & w.

namespace HeightWeightInfo
{
    class File1
    {
    }
    public partial class Record
    {
        private int h;
        private int w;
        public Record(int h, int w)
        {
            this.h = h;
            this.w = w;
        }
    }
}

Here is another file named as File2.cs with the same partial class Record which has only the method PrintRecord. This method will display the values of h & w.

namespace HeightWeightInfo
{
    class File2
    {
    }
    public partial class Record
    {
        public void PrintRecord()
        {
            Console.WriteLine("Height:"+ h);
            Console.WriteLine("Weight:"+ w);
        }
    }
}

Here now we can see the main method of the project:

namespace HeightWeightInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            Record myRecord = new Record(10, 15);
            myRecord.PrintRecord();
            Console.ReadLine();
        }
    }
}

Here we have the object of the class Record as myRecord which is passing the parameter values as 10 and 15 to h and w respectively to the method defined in File1.cs.

The method PrintRecord is called by the object myRecord which is defined in the File2.cs.

This shows that the partial keyword helps to combine all the attributes of a class defined in various files to work as a single class.

Places where partial class can be used:

  1. While working on a larger projects with more than one developer, it helps the developers to work on the same class simultaneously.
  2. Codes can be added or modified to the class without re-creating source files which are automatically generated by the IDE (i.e. Visual Studio).

Things to Remember about Partial Class

The partial keyword specify that other parts of the class can be defined in the namespace. It is mandatory to use the partial keyword if we are trying to make a class partial. All the parts of the class should be in the same namespace and available at compile time to form the final type. All the parts must have same access modifier i.e. private, public, or so on.

  • If any part is declared abstract, then the whole type is considered abstract.
  • If any part is declared sealed, then the whole type is considered sealed.
  • If any part declares a base type, then the whole type inherits that class.
  • Any class member declared in a partial definition are available to all other parts.
  • All parts of a partial class should be in the same namespace.

**Note: The partial modifier is not available on delegate or enumeration declarations


Introduction to Partial Methods

A partial class may contain a partial method. One part of the class contains the signature of the method. An optional implementation may be defined in the same part or another part. If the implementation is not supplied, then the method and all calls are removed at compile time.

Example 2:

Let's take an example as a partial class Car defined in file1.cs which has three methods InitializeCar(), BuildRim() and BuildWheels(). Among those methods, InitializeCar is defined as partial.

public partial class Car
{
    partial void InitializeCar();
    public void BuildRim() { }
    public void BuildWheels() { }
}

And we have another file named as file2.cs which has two methods BuildEngine and InitializeCar. The method InitializeCar is partial method which is also defined in file1.cs.

public partial class Car
{
    public void BuildEngine() { }
    partial void InitializeCar()
    {
        string str = "Car";
    }
}

A partial method declaration consists of two parts:

  1. The definition as in file1.cs.
  2. The implementation as in file2.cs.

They may be in separate parts of the partial class, or in the same part.


Things to remember about Partial Method

  • partial keyword.
  • return type void .
  • implicitly private.
  • and cannot be virtual.