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.

This post was originally posted at https://agrawalsuneet.github.io/blogs/partial-class-in-csharp-partial-modifier/ and reposted on Medium on 30th Mar 2020.

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?

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

  • 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

  • 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.

That’s all for now. You can read my other interesting posts here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store