20
DecPartial Methods in C Sharp
Partial Methods: An Overview
A partial method is a special method that exists within a partial class or structure. One part of the partial class or struct has 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 the declared partial method, the method and all calls to that partial method will be removed at compile time. However, in this C# Tutorial, we will explore more about Partial Methods which will include, partial methods in c# with its example, the need of partial methods, the requirement of partial methods in c#, and, also when partial methods are used in C#.
Why are partial methods required?
Partial methods are particularly helpful for customizing auto-generated code by the tool. Whenever the tool generates the code then the tool may declare some partial method and implementation of these methods is decided by the developers.
If you are using an entity framework for making DAL then you have seen that Visual Studio makes a partial method OnContextCreated() as shown below. Now the implementation of it depends on you whether you want to use it or not.
Example:
Let's elaborate partial method in C# Compiler with its example:
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");
}
}
Read More - C Sharp Interview Questions
Key points about the partial method
Partial methods can be declared or defined within the partial class or struct.
Partial methods are implicitly private and declarations must have partial keywords.
Partial methods must return void.
Partial methods implementation is optional.
Partial methods can be static unsafe and generic.
Partial methods can have ref parameters but not out parameters since these can't return value.
You can also make delegate to a partial method that has been defined and implemented. If the partial method is only defined, you can not.
The signatures of the partial method will be the 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);
}
}
Conclusion
I hope you will enjoy these tricks while programming C#. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome. Also, Consider our C# Programming Course for a better understanding of all C# concepts.
FAQs
Take our Csharp skill challenge to evaluate yourself!
In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.