Blog / March 29, 2020 / 3 mins read / By Suneet Agrawal

Partial class in C# (partial modifier)

Think about a situation where two or more developers need to modify the same class or add new functionalities to the same class. Or assume they need to implement interfaces to the same class. There will be a merge conflict which needs to be resolved manually or using some tool.

Or think about a situation where an automatically generated code needs to be added into some class without having to recreate the source file.

A similar kind of challenge We faced while working on a Unity project where we two of the devs were constantly working on some same classes. The only way to avoid conflicts was by separating the areas in the class where we both will add the functionalities. Later I came to know about the partial class which was helpful in such situations.

What is a Partial class?

A partial class is a special type of class where a single class can be split into multiple .cs file and while compilation, all the functionalities of same partial classes written in different .cs files will be merged and then compile it.

It provides you with functionality where you can split your class into different files based on the requirement and usability.

In short, partial class in a way of writing a single class into multiple .cs files and these files will be combined 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.

//Someclass1.cs
public partial class SomeClass
{
    // code
    private void function1(){ }
}


//Someclass2.cs
public partial class SomeClass
{
    // code
    private void function2(){ }
}


//final compiled code
public class SomeClass
{
    private void function1(){ }
    private void function2(){ }
}

Requirements of Partial modifier

  • A partial modifier can be added to a class or an interface or a struct.
  • Every part of the partial class definition should be in the same assembly and namespace, but we can use different source file name.
  • All the parts must have the same accessibility for example public or private, etc.
  • If any part is declared abstract, sealed or base type then the whole class is declared of the same type.
  • The partial modifier can only appear immediately before the keywords class, struct, or interface.
  • Nested partial types are allowed.
  • Different parts can have different base types like implementing different interfaces and the final class will inherit all the base types.

Advantages of Partial class

  • With the concept of partial classes, we can better the code structure like by separating business logic and UI implementation within the same class.
  • Can avoid conflicts if multiple developers working on the same class or interface or struct.
  • When we working with automatically generated source, code can be added to the class without having to recreate the source file. For example, Visual Studio separates HTML code for the UI and server-side code into two separate files: .aspx and .cs files.
Comments