C# Partial Class and Partial Method: In this tutorial we will learn about how and why fractional class and partial methods be implemented in C#.
Partial classes gives us a choice to split classes into various parts and in numerous source files. All parts are combined into one single class during compile time. All parts ought to contain the watchword partial,should be of a similar accessibility. All parts ought to be available in a similar assembly for it to be incorporated during compile time.
There are numerous circumstances when you may have to part a class definition, for example, when dealing with a huge scope projects, different developers and programmers may have to chip away at a similar class simultaneously. For this situation we can use a feature called Partial Class.
In this article, you will learn-
Introduction to Partial Class
While programming in C# (or OOP), we can part the meaning of a class more than at least two source files. The source files contains a segment of the meaning of class, and all parts are combined when the application is compiled. For parting a class definition, we need to use the partial catchphrase.
Example 1:
We have a project named as HeightWeightInfo which shows height and weight.
We have a file named as File1.cs with a partial class named as Record. It has two integer variables h and w and a method/constructor named as Record which is allocating the values of h & w.
namespace HeightWeightInfo { class File1 { } public partial class Record { private int h; private int w; public Record(int h, int w) { this.h = h; this.w = w; } } }
Here is another file named as File2.cs with a similar partial class Record which has just the method PrintRecord. This method will show the values of h & w.
namespace HeightWeightInfo { class File2 { } public partial class Record { public void PrintRecord() { Console.WriteLine("Height:"+ h); Console.WriteLine("Weight:"+ w); } } }
Here now we can see the main method for the project:
namespace HeightWeightInfo { class Program { static void Main(string[] args) { Record myRecord = new Record(10, 15); myRecord.PrintRecord(); Console.ReadLine(); } } }
Here we have the object of the class Record as myRecord which is passing the parameter values as 10 and 15 to h and w respectively to the method defined in File1.cs.
The strategy PrintRecord is called by the object myRecord which is defined in the File2.cs.
This shows that the partial catchphrase assists with combine all of the attributes of a class defined in different files to work in as a single class.
Places where partial class can be used:
- While working on a larger projects with more than one developer, it assists the developers with dealing with a similar class all the while.
- Codes can be added or altered to the class without re-creating source files which are consequently created by the IDE (for example Visual Studio).
Things to Remember about Partial Class
The partial watchword determine that different pieces of the class can be defined in the namespace. It is mandatory to use the partial catchphrase on the off chance that we are attempting to make a class partial. All the pieces of the class ought to be in the equivalent namespace and accessible at compile time to form the last type. All the parts should have same access modifier for example private, public, or so on.
• If any part is declared abstract, at that point the entire sort is considered abstract.
• If any part is declared sealed, at that point the entire type is considered sealed.
• If any part declared a base sort, at that point the entire type inherits that class.
• Any class part declared in a partial definition are accessible to any remaining parts.
• All parts of a partial class ought to be in the equivalent namespace.
**Note: The partial modifier isn’t accessible on delegate or enumeration declarations
Introduction to Partial Methods
A partial class may contain a partial strategy. One piece of the class contains the signature of the strategy. An optional execution might be defined in a similar part or another part. On the off chance that the execution isn’t provided, the strategy and all calls are eliminated at compile time.
Example 2:
Let’s take an example as a partial class Car defined in file1.cs which has three strategies InitializeCar(), BuildRim() and BuildWheels(). Among those strategies, InitializeCar is defined as partial.
public partial class Car { partial void InitializeCar(); public void BuildRim() { } public void BuildWheels() { } }
What’s more, we have another file named as file2.cs which has two techniques BuildEngine and InitializeCar. The technique InitializeCar is partial strategy which is additionally defined in file1.cs.
public partial class Car { public void BuildEngine() { } partial void InitializeCar() { string str = "Car"; } }
A partial strategy declaration comprises of two parts:
- The definition as in file1.cs.
- The implementation as in file2.cs.
They might be in separate pieces of the partial class, or in a similar part.
Things to remember about Partial Method
• partial catchphrase.
• return type void.
• implicitly private.
• and can’t be virtual.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.