TechCognition

Microsoft Visual Studio 2017 Promo Code
Microsoft Office 2016 Promo Code

Introduction to Partial methods in C#

Partial methods introduced in C# 3.0. Partial methods should belong to partial classes only. A partial method consists two parts, Definition & Implementation. If the partial method does not contain implementation and contains the only definition, then definition will not be compiled. To discuss partial methods with a simple example, open Microsoft Visual Studio 2015 and create a console application. Add a new class and name it as Company.cs. Declare Company class as partial and add GetCompanyInfo() method as a partial method as below.

partial class Company

{

        partial void GetCompanyInfo(int id);

} 

partial class Company

{

        partial void GetCompanyInfo(int id)

        {

            //Add method body here

        }

}

First Company partial class, we have declare the partial method GetCompanyInfo() and second Company partial class has GetCompanyInfo() method with body.

To declare partial methods, we have to follow below formulas.

  1. Only partial class can contain partial methods

  2. Partial method should be void type means partial method should not return any value

  3. Partial method should be declared as private

  4. Partial method can be without implementation

  5. Partial method can be static

  6. We cannot invoke partial methods using delegates
     

Add comment

Loading

© Copyright 2017 www.techcognition.com | Sitemap