c# partial class. What is a partial class in C# ?

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Never miss a post!

Sign up for our newsletter and get FREE Development Trends delivered directly to your inbox.

You can unsubscribe any time. Terms & Conditions.
Categories

A C# partial class is a functionality of C# that allows the implementation of a class in multiple files. Even if the class is defined into multiple files the compiler will combine the implementation of the class into a single class. Implementation of a partial class is done using the partial keyword.

Implementation of RecordPartialDef.cs

public partial class RecordPartial 
{ 
    int RecordId { get; set; }   
    string Name { get; set; }   
    string Surname { get; set; } 
}

Implemention of RecordParitalImp.cs

public partial class RecordPartial 
{ 
     public void AddRecord() { } 
     public void DeleteRecord() { } 
}

When to use a partial class?

In the following section, we will cover the different use cases when to opt for a partial class.

  1. The most used use case of partial classes is when code is being auto-generated. A partial class gives us the opportunity to easily extend a class that has been generated through code. Code that is modified manually is not overwritten if the code is regenerated. This allows segregation of concerns between auto-generated code and code extensions.
  2. A useful feature of partial classes is in a scenario when a class implements multiple interfaces. To make code more readable to can implement each interface into a separate file.
  3. Refactoring of God classes. A god class is a design pattern where a class is responsible for a lot of things. Such classes are hard to manage as they will contain a lot of code. Splitting a God class into multiple files will allow you to segregate responsibilities into separate files. The added advantage is that it simplifies the processes of refactoring a God classes without going through a shotgun surgery.
  4. Partial classes simplify the collaboration between multiple developers as multiple developers can be working on the same class but manipulating different files. Reducing the time-consuming process of merging conflicts.

Partial Class Rules

  1. The partial modifier must be placed before the class, struct or interface keywords
  2. A partial class has to reside in the same namespace and assembly.
  3. All the partial classes that build the class must be assigned the same accessibility ie. all public, private, protected.
  4. Different classes can inherit from different base classes or implement different interfaces. The final class will be the aggregate of all the classes.
  5. You can implement nested partial classes 
  6. If one of the partial classes is marked as abstract or sealed then the resulting class will be assigned the class type.

Partial functions

A partial class can contain a partial function. As with the partial classes and partial function require a partial modifier before the function name. A partial function allows a developer to define a signature of a function that can be implemented in another partial class. The implementation of the partial function is optional. The compiler will remove the function from the resulting class if there is no implementation.

Partial functions are simplicity private and you cannot assign them a different modifier.

Partial functions require a void return type.

Partial functions support in or ref parameter but not out parameters.

public partial class MyPartialClass
{ 
     partial void MyPartialFunction(); 
}
public partial class MyPartialClass
{ 
     // Implementation partial 
     void MyPartialFunction() { } }
}

 

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Our website uses cookies that help it to function, allow us to analyze how you interact with it, and help us to improve its performance. By using our website you agree by our Terms and Conditions and Privacy Policy.