Partial Class In C#

Background

In this article, we will learn about one of the most reusable object-oriented features of C#, Partial Classes. We will learn about Partial Classes from the basics because I have written this article focusing on students and beginners. Before proceeding further, please refer to my previous articles for a better understanding.

What is partial?

It is the type of class that allows dividing  methods into multiple class source files and at compile time, these files are combined into one single class. A partial class allows the definition of methods into two different class source files and at execution time, these files are combined together.

 

Some key points about Partial Classes

  • All the parts of a partial class must be prefixed with the partial keyword.
  • If you seal any specific part of a partial class, then the entire class is sealed; the same as in an abstract class. 
  • Inheritance cannot be applied to partial classes.

To demonstrate more about partial classes, let us create a simple console application by creating two partial classes into two different class source files, as in:

  1. Open Visual Studio from "Start" -> "All programs" -> "Microsoft Visual Studio".
  2. Then, go to "File" -> "New" -> "Project..." then select "Visual C#" -> "Windows" -> "Console application".
  3. Then, specify the name such as Partial class or whatever name you wish and the location of the project and click on the "OK" button. The new project is created.
  4. Right-click on Solution Explorer and add two class files. After adding them, the Solution Explorer will look like below.

Partial Class In C#
Now, open the class1.cs file and create the following Partial class named "car" and create method1 inside it, as in, 
  1. partial class car  
  2.    {  
  3.      public string Method1()  
  4.      {  
  5.          return "This is  method 1 of class car";  
  6.      }  
  7. }   
Now, open the class2.cs file and create the following Partial class named car and create method2 inside it, as in,
  1.  partial class car  
  2.    {  
  3.      public string Method2()  
  4.      {  
  5.          return "This is  method 2 of the class car";  
  6.   
  7.      }  
  8. }   
Now, create the object of partial class Car in the class named program to access the Methods of both classes:
 
Partial Class In C# 
 
In the preceding diagram, we can see that we have created Method1 and  Method2 methods in two different class files but in spite of this we can access both methods by creating an object of the car class, so this type of class is called a Partial Class.
 
The following is the code of the Program class, 
  1. public class Program  
  2.   {  
  3.       static void Main(string[] args)  
  4.       {  
  5.          car obj = new car();  
  6.          Console.WriteLine( obj.Method1());  
  7.          Console.WriteLine(obj.Method2());  
  8.          Console.ReadLine();  
  9.   
  10.       }  
  11.   }  
Now, run the application. The output will be as given below.
 
Partial Class In C# 
 
Hence, from the preceding examples, it's clear that a partial class allows dividing the methods into multiple class source files and at compile time, these files are combined into one single class.
 
Note
Download the Zip file from the attachment for the full source code of the application.
 

Summary


In the preceding article, I have briefly explained the use of partial classes to make them understandable to beginners and newcomers. If you have any suggestion regarding this article, then please comment below.


Similar Articles