Partial Class in C#

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 partial is
It is the type of class that allows dividing their methods into multiple class source files and at compile time these files are combined into one single class.
A partial class allows definition of methods into two different class source files and at execution time these files are combined together.
Some key points
  • 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 the 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, Solution Explorer will look such as follows:


Now open the class1.cs file and create the following one Partial class named car and create method1 inside it, as in:
  partial class car
     {
       public string Method1()
       {
           return "This is  method 1 from class car";

       }
  }
Now open the class2.cs file and create the following one Partial class named car and create method2 inside it, as in:
   partial class car
     {
       public string Method2()
       {
           return "This is  method 2 from class car";

       }
  }
 Now create the object of partial class Car in the class Named program to access the Methods of both classes as in:

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:
  public class Program
    {
        static void Main(string[] args)
        {
           car obj = new car();
           Console.WriteLine( obj.Method1());
           Console.WriteLine(obj.Method2());
           Console.ReadLine();

        }
    }
Now run the application, the output will be as follows:
Hence, from the preceding all 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.
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 contact me.
I

2 comments

There is a typo in the example as there are twice public string Method2() in both class1 and class2 ;)

Reply

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode