Partial Method in C#

A partial class or struct may contain a partial method. Similar to a partial class, a partial method can be used as a definition in one part while another part can be the implementation. If there is no implementation of the partial method then the method and its calls are removed at compile time. 

A partial method declaration consists of two parts: the definition and the implementation. 

The code listing in Listing 3 defines a partial method called InitializeCar in one .cs file and the implementation of the same method in a different .cs file. 

public partial class Car
{
    // A partial method definiton
    partial void InitializeCar(); 

    // Car Exterior Functionality
    public void BuildTrim() { }
    public void BuildWheels() { }
} 

public partial class Car
{

    /// Car Engine
    public void BuildEngine() { } 

    // A partial method implementaion
    partial void InitializeCar()
    {
        string str = "Car";
        // Put all car initialization here
    } 

}

Listing 3

Up Next
    Ebook Download
    View all
    Learn
    View all