Partial Methods in C Sharp

Partial Methods in C Sharp

23 May 2024
Intermediate
118K Views
3 min read
Learn with an interactive course and practical hands-on labs

Best Free C# Course: Learn C# In 21 Days

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

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

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

  3. Partial methods must return void.

  4. Partial methods implementation is optional.

  5. Partial methods can be static 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 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 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

It is a feature that allows developers to split the definition of a class or a struct, an interface, or a method over two or more source files.

void or anon-awaitable async void, which limits their usefulness.

Partial types can also declare partial Methods, which can be used to advertise the availability of a method in one part, and optionally provide an implementation on the other part. 

In this, Rather than cluttering a single file with thousands of lines of code, you separate them into different files based on functionality, 

Yes. it must be declared first(like an abstract method), with a signature only and no definition. 

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.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this