Partial Class in C#

Partial Class allows us to split a class into 2 or more files, all these parts are then combined into a Single Class, when application is compiled. The Partial keyword can also be used to split a structure or an interface over two or more files.
We can use partial classes when developers are working on large projects. A partial class distributes a class over multiple seperate files allowing developers to work on the class simultaneously. Using the Partial keyword we can declare a class as partial.
Advantages of partial classes

  • The main advantage of partial class is that visual studio uses partial classes to seperate automatically generated system code from developers code.Webform.aspx.cs contains the developer code.
  • Webform.aspx.designer.cs contains the system generated code for example declarations for the controls that you drag and drop on the web form.
  • When working on large projects spreading a class over seperate files allow multiple programmers to work on it simultaneously.
Points to remember
  • All the parts spread across different files , must use the partial keyword.
  • All the parts spread across different files , must have the same access modifiers.
  • If Any of the part declared abstract then the entire type is considered abstract.
  • If Any of the part declared sealed then the entire type is considered sealed.
  • If any of the parts inherit a class, then entire type inherits that class
  • C# does not support multiple class inheritance , different parts of the partial class , must not specify different base classes.
  • Different parts of the partial class can specify different base interfaces, and the final type implements all of the interfaces listed by all of the partial declarations.
  • Any members that are declared in a partial defination are available to all of the other parts of the partial class.

What is Partial Method in C#?

A partial method is created using the Partial keyword. A partial class or structure can contain the partial methods.
Points to Remember

  • A partial method declaration consists of two parts first, The defination(only the method signature) and second, the implementation. these may be in seperate parts of partial class or in the same part
  • The implementation for a partial method is optional , if we do not provide the implementation, the compiler removes the signature and calls the method
  • Partial methods are private by default and it is compile time error to include any access modifier including private
  • It is compile error to include declaration and implementation at the same time for a partial method.
  • Signature of the partial method declaration, must match with the signature of the implementation.
  • A partial method return type must be void, including any other return type is a compile time error.
  • A partial method must be declared within a partial class or partial structure. a non partial class or struct can not include partial methods.
  • A partial method can be implemented only once,trying to implement more than once , raise a compile time error.

Image is not available

About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server, EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.