Tech Point Fundamentals

Sunday, September 20, 2020

Partial Classes with Real Examples in C#

Partial Classes and Real Usage of Partial Classes in C#


  If you have worked with C#, or perhaps even other programming languages, you are used to know the fact that the name of a class should be unique i.e there cannot be two classes with the same name unless they are in different namespaces. In this article, we will see how the .Net team resolved this issue by partial types.


When we were using the original version of C# with the .NET framework version 1.1 or earlier, classes, structures, and interfaces must each be created within a single file. It was possible to include more than one type in a single ".cs" file but a single type's code cannot be split into sections into multiple separate files.

However, at one moment, Microsoft decided to overcome the above issue, with the introduction of something called partial types. So partial class, partial interface, and the partial structure were introduced in C# 2.0.

Now it is possible to split the definition of a class, interface, or structure over more than one source file.

Partial Types

In C#, you can split the implementation of a class, interface, or struct in multiple files using the partial keyword.  The partial keyword is used to create partial types. The partial keyword indicates that other parts of the class, interface, struct, or method can be defined anywhere in the namespace

When you define your class, interface, or struct with the partial keyword, you or someone else is allowed to extend the functionality of your class with another class, which also needs to be declared as partial.

All the parts must use the partial keyword and must be available at compile time to form the final type.  The compiler combines all the implementation from multiple .cs files when the program is compiled.

Please note that the partial modifier can only be used just before the keywords struct, class, interface, and void. The partial modifier is not available on delegate or enumeration declarations.

Partial Class

A class declared with a partial keyword is called a partial class. A partial class is a special feature of C# which provides the ability to implement the functionality of a single class into multiple code files.

All these files are combined into a single class file when the application is compiled, so all the parts must be available at compile time.



Fundamental Rules of Partial Class

    1. The partial modifier must be used just before the class keyword name.
    2. All the parts must be declared with a partial keyword, otherwise, it causes a compile-time error.
    3. Every part must be defined in the same namespace (assembly, dll, or exe).
    4. Each part must be available at compile time to form the final type.
    5. You can also have a constructor and destructor in a partial class.
    6. A partial class can have partial methods.
    7. All the parts must have the same accessibility (public, private, protected, etc).
    8. If any part is declared as abstract, then all the part is considered abstract. 
    9. If any part is declared as sealed, then all the part is considered sealed.
    10. Inheritance between the partial parts is not allowed.
    11. The class member declared in a partial definition will be available to all the other parts.
    12. If any part declares a base type, then all the other parts also inherit that class automatically. 
    13. All the parts can specify different base interfaces, but the final type implements all the interfaces listed by all the partial declarations.
    14. Nested partial types are allowed.
    15. All the parts can specify different class attributes (like SerializableAttribute, ObsoleteAttribute), but all the attributes are merged at compile time.
    16. At compile time XML-Comments of different parts will be merged.


Advantage of Partial Class

  1. With the help of partial classes, multiple developers can work simultaneously on the same class in different files.
  2. With the help of partial classes, you can split the UI of the design code and the business logic code to read and understand the code.
  3. You can also maintain and manage your application in an efficient manner by compressing large classes into small ones.
  4. 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 the visual studio.

Real Usage and Examples of Partial Classes

The visual studio uses partial classes to separate automatically generated code from the developer's code. Following are the real examples of partial classes:

Web Form Application

In the Web Form Applications, visual studio use partial classes for creating aspx files. Each aspx file contains two files with partial classes; one is "aspx.cs" and other is "aspx.designer.cs". The ".aspx.cs" file contains the code behind files while "aspx.designer.cs" file contains web form design components.


Window Form Application

In the Windows Form Applications, the visual studio uses partial classes for creating windows forms. Each windows form contains a ".Designer.cs" with a partial class.  The ".Designer.cs" file contains the windows form design components.


ADO.Net Entity Data Model

When you add any ADO.Net Entity Data Model, along with the ".edmx" file visual studio creates different partial classes in the different source files.

For example under "" it creates".Context.cs" with the partial class which contains all the database schema-related codes and configurations.

Also under the ".tt" file, it creates partial classes for each and every database object. 



In LINQ to SQL Classes, when you add any database object into ORD (Object Relational Designer), the visual studio also creates a ".designer.cs" also along with the ".dbml" file which contains partial classes. Also, for each object of ORD (like database tables, stored procedures, functions) there is a corresponding partial class into the ".designer.cs" file. This file also contains a lot of partial methods related to the database as well. 


Data Set Designer - XSD file (XML Schema Definition Language)

When you add any Dataset Designer file (.xsd file) in the project, the visual studio creates a ".Designer.cs" file along with the ".xsd" file which contains auto-generated partial classes. 
Also for each item of the designer, there is a corresponding partial class in this ".Designer.cs" file. 


WPF User Controls 

When you add any WPF user controls, along with the ".xaml" file, the visual studio also creates an auto-generated code-behind file ".xaml.cs" with partial classes. 


Live Example


  1. The partial keyword can be used to split the implementation of a class, an interface, a struct, a method in multiple source files.
  2. The partial types were introduced in C# 2.
  3. The partial modifier can only appear immediately before 'class', 'struct', 'interface', or 'void'.
  4. The partial keyword is not available for delegate and enum.
  5. Every subpart should be defined with a partial keyword.
  6. Every subpart should be defined in the same namespace.

Recommended Articles

Thanks for visiting this page. Please follow us on Twitter, Facebook, LinkedIn, Youtube, Quora, and Pinterest for regular updates.

Video Recommendation

Watch More Videos...

No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.