Introduction to Partial in C#
A special feature of C# is a partial class using which one can be able to implement the single class functionality into multiple files which are later clubbed together to form a single class file during the compilation of the application and the partial keyword is used to create a partial class in C# and this partial keyword is also used to divide the methods functionalities, interfaces functionalities or structure’s functionalities into multiple files and all these files must be available during the time of compilation compulsorily for the creation of the final file and the user can also make use of nested partial types.
Syntax:
public partial Class_name
{
// code
}
Functions of Partial Class in C#
The partial keyword must be used before any class name in order to make the class a partial class. An interface, structure or class can be divided into multiple parts using the partial keyword. A single class can be divided into multiple files by making the class as a partial class. During the compilation of the partial code, multiple classes or multiple interfaces or structure is compiled into a single class or single interface or structure. The user interface code for design can be separated from the code for business logic using partial keyword and this makes working and understanding easier.
Using the partial class, multiple developers can work in parallel. The customized logic code can be embedded into auto-generated code by the framework by using partial classes. Larger classes can be easily understood and maintained by dividing them into smaller classes. Application development can be made faster by dividing the interfaces into multiple codes which can be shared with multiple developers. Sealing a partial class causes the entire class to be sealed. This is called the sealed property of the partial class. Making a partial class abstract causes the entire class to be an abstract class. This is called Abstract property of the partial class. The partial classes with the same name must be declared under the scope of the same namespace only.
Examples to Implement Partial in C#
Consider the following example to understand the concept of partial class in C#:
Example #1
Code:
using System
public class Check
{
//main method is called
public static void Main()
{
//the same partuial class is defined at two places twice but during compilation it is executed as a single file
parclass pc=new parclass();
pc.firstmethod();
pc.secmethod();
}
//partial class is defined with the same class name
public partial class parclass
{
//a method is declared
public void firstmethod()
{
Console.WriteLine("The first method is called");
}
}
//another partial class is defined with the same name
public partial class parclass
{
//another method is declared
public void secmethod()
{
Console.WriteLine("The second method is called");
}
}
}
Output:
Explanation: In the above program, a class called check is defined within which the main method is called. This main method consists of the instance of the partial classes which are defined later using which the methods of the partial classes are called. Two partial classes with the same name parclass are defined. They contain different methods within them which are called in the main method. The partial class combines the multiple classes into a single class during the compilation and the output is as shown in the snapshot above.
Example #2
C# program to demonstrate the partial classes while assigning values to variables and printing them using two different classes.
Code:
using System;
public class Check
{
//main method is called
public static void Main()
{
//the same partial class is defined at two places twice but during compilation it is executed as a single file
rec r=new rec(5,10);
r.print();
Console.ReadLine();
}
//partial class is defined with the same class name
public partial class rec
{
private int a;
private int b;
//a method is declared
public rec(int a, int b)
{
this.a = a;
this.b = b;
}
}
//another partial class is defined with the same name
public partial class rec
{
//another method is declared
public void print()
{
Console.WriteLine("The value of a is "+ a);
Console.WriteLine("The value of b is "+ b);
}
}
}
Output:
Explanation: In the above program, a class called check is defined within which the main method is called. This main method consists of the instance of the partial classes which are defined later using which the methods of the partial classes are called. Two partial classes with the same name rec are defined. They contain different methods within them which are called in the main method. The partial class combines the multiple classes into a single class during the compilation and the output is as shown in the snapshot above.
Example #3
C# program to demonstrate the partial classes while assigning values to variables and printing them using two different classes.
Code:
using System;
public class Check
{
//main method is called
public static void Main()
{
//the same partial class is defined at two places twice but during compilation it is executed as a single file
stat r=new stat();
r.print1();
r.print2();
}
//partial class is defined with the same class name
public partial class stat
{
public void print1()
{
Console.WriteLine("Hello, welcome to Partial class one");
}
}
//another partial class is defined with the same name
public partial class stat
{
//another method is declared
public void print2()
{
Console.WriteLine("Hello, welcome to partial class two");
}
}
}
Output:
Explanation: In the above program, a class called check is defined within which the main method is called. This main method consists of the instance of the partial classes which are defined later using which the methods of the partial classes are called. Two partial classes with the same name stat are defined. They contain different methods within them which are called in the main method. The partial class combines the multiple classes into a single class during the compilation and the output is as shown in the snapshot above.
Recommended Articles
This is a guide to Partial in C#. Here we discuss the concept of partial class in C# through definition, the syntax of partial class in C#, working of partial class in C# through examples and their outputs. You can also go through our other suggested articles to learn more –