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 other part can be the implementation. If there is no implementation of partial method, 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 below defines a partial method called InitializeCar in a different .cs file and implementation of the same method is 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
    }

}

Advertisements
By Sriramjithendra Posted in C#.NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s