Please enable Javascript to correctly display the contents on Dot Net Tricks!

Partial Methods in C Sharp with example

 Print 
  Author : Shailendra Chauhan
Posted On : 16 Oct 2012
Total Views : 102,731   
Updated On : 24 Sep 2016
 

A partial method is a special method that exist with in a partial class or struct. One part of the partial class or struct have only partial method declaration means signature and another part of the same partial class or struct may have implementation for that partial method. If the implementation is not provided for declared partial method, the method and all calls to that partial methods will be removed at compile time. For more about Partial Class, Interface or Struct refer the article Partial Class, Interface or Struct in C Sharp with example

Why partial methods required ?

Partial methods are particularly helpful for customizing auto generated code by the tool. Whenever the tool generate the code then tool may decalare some partial method and implementation of these methods is decided by the developers.

If you are using entity framework for making DAL then you have seen that the Visual Studio make a partial method OnContextCreated() as shown below. Now the implementation of it depends on you whether you want to use it or not.

 public partial class DALEntities : ObjectContext
{
 #region Constructors
 // Constructors for DALentities
 #endregion 
#region Partial Methods 
partial void OnContextCreated(); 
#endregion 
} 
 // This part can be put in the separate file
public partial class DALEntities : ObjectContext
{
 partial void OnContextCreated()
 { 
 // put method implementation code 
 Debug.WriteLine("OnContextCreated partial method"); 
}
} 

Key points about partial method

  1. Partial methods can be declared or defined with in the partial class or struct.

  2. Partial methods are implicitly private and declarations must have partial keyword.

  3. Partial methods must return void.

  4. Partial methods implementation is optional.

  5. Partial methods can be static and unsafe and generic.

  6. Partial methods can have ref parameters but not out parameters since these can't return value.

  7. You can also make a delegate to a partial method that has been defined and implemented. If the partial method is only defined, you can not.

  8. The signatures of partial method will be same in both parts of the partial class or struct.

 partial class Example 
{ 
partial void ExampleMethod(string s); 
}
 // This part can be put in the separate file 
partial class Example { //Implement the method 
partial void ExampleMethod(String s)
 {
 Console.WriteLine("Your string: {0}", s);
 }
} 
What do you think?

I hope you will enjoy this tricks while programming C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS
20 SEP
MEAN Stack (Online)
07:00 AM-09:00 AM IST
20 SEP
ASP.NET MVC with Angular4 (Online)
9:00PM- 11:00PM IST(+5:30GMT)
16 SEP
Angular2 and Angular4 (Online)
08:00 AM-10:00 AM IST(+5.30 GMT)
16 SEP
Microsoft Azure Infrastructure Solutions (Online)
08:00 PM-11:00 PM IST / 9:30 AM-12:30 PM CST
7 SEP
ASP.NET MVC with Angular4 (Online)
09:00 PM to 11:00 PM IST (+5:30 GMT)
22 AUG
ASP.NET Core with Angular4 (Online)
07:00 AM - 9:00 AM IST(+5:30 GMT)
19 AUG
MEAN Stack 2 (Online)
5:30 Pm - 7:00 PM IST (+5:30 GMT)
19 AUG
ASP.NET MVC with Angular4 (Online)
10:30 Am to 12:30 PM IST (+ 5:30 GMT)
19 AUG
NodeJS with Angular4 (Classroom)
04:00 PM to 07:00 PM IST (+5:30 GMT)
12 AUG
ASP.NET MVC with Angular4 (Classroom)
11:30 AM - 1:30 PM IST (+5:30GMT)
15 JUL
ASP.NET MVC with Angular4 (Online)
03:30 PM - 05:30 PM IST(+5.30 GMT)
LIKE US ON FACEBOOK
 
+