A partial class is a special feature of C#. It provides a special ability to implement the functionality of a single class into multiple files and all these files are combined into a single class file when the application is compiled. A partial class is created by using a partial keyword. This keyword is also useful to split the functionality of methods, interfaces, or structure into multiple files.
Syntax :
public partial Clas_name
{
// code
}
Important points:
- When you want to chop the functionality of the class, method, interface, or structure into multiple files, then you should use partial keyword and all the files are mandatory to be available at compile time for creating the final file.
- The partial modifier can only present instantly before the keywords like struct, class, and interface.
- Every part of the partial class definition should be in the same assembly and namespace, but you can use a different source file name.
- Every part of the partial class definition should have the same accessibility as private, protected, etc.
- If any part of the partial class is declared as an abstract, sealed, or base, then the whole class is declared of the same type.
- The user is also allowed to use nested partial types.
- Dissimilar parts may have dissimilar base types, but the final type must inherit all the base types.
Example: Here, we are taking a class named Geeks and split the definition of Geeks class into two different files named Geeks1.cs, and Geeks2.cs as shown below:
In Geeks1.cs, and Geeks2.cs, a partial class is created using the partial keyword and each file contains different functionality of Geeks class as shown below.
Geeks1.cs
C#
public partial class Geeks {
private string Author_name;
private int Total_articles;
public Geeks(string a, int t)
{
this.Authour_name = a;
this.Total_articles = t;
}
}
Geeks2.cs
C#
public partial class Geeks {
public void Display()
{
Console.WriteLine("Author's name is : " + Author_name);
Console.WriteLine("Total number articles is : " + Total_articles);
}
}
When we execute the above code, the compiler combines Geeks1.cs and Geeks2.cs into a single file, i.e. Geeks as shown below.
Geeks This class may contain the Main Method. For simplicity, here Main() method is not included.
C#
public class Geeks {
private string Author_name;
private int Total_articles;
public Geeks(string a, int t)
{
this.Authour_name = a;
this.Total_articles = t;
}
public void Display()
{
Console.WriteLine("Author's name is : " + Author_name);
Console.WriteLine("Total number articles is : " + Total_articles);
}
}
Advantages :
- With the help of partial classes, multiple developers can work simultaneously in the same class in different files.
- With the help of a partial class concept, you can split the UI of the design code and the business logic code to read and understand the code.
- When you were working with automatically generated code, the code can be added to the class without having to recreate the source file like in Visual studio.
- You can also maintain your application in an efficient manner by compressing large classes into small ones.
Similar Reads
Partial Methods in C# C# contains a special method is known as a partial method, which contains declaration part in one partial class and definition part in another partial class or may contain both declaration and definition in the same partial class. Basically, partial methods exist in the partial class, or in the stru
2 min read
C# StringCollection Class In C#, the StringCollection class is part of the System.Collections.Specialized namespace. It provides a collection of strings that allows us to store and manage a list of strings easily. This class is a type-safe collection that automatically resizes as needed.StringCollection class accepts null as
5 min read
Null-Coalescing Operator in C# In C#, ?? operator is known as Null-coalescing operator. It will return the value of its left-hand operand if it is not null. If it is null, then it will evaluate the right-hand operand and returns its result. Or if the left-hand operand evaluates to non-null, then it does not evaluate its right-han
4 min read
Lambda Expressions in C# Lambda expressions in C# are used like anonymous functions, with the difference that in Lambda expressions you don't need to specify the type of the value that you input thus making it more flexible to use. The '=>' is the lambda operator which is used in all lambda expressions. The Lambda expres
3 min read
C# Tuple<T1,T2> Class The Tuple<T1, T2> is used to create a 2-tuple or pair. It represents a tuple which contains the two elements in it. We can instantiate a Tuple<T1, T2> object by calling either the Tuple<T1, T2>(T1, T2) constructor or the static Tuple.Create method. We can retrieve the value of the
2 min read