Partial Classes and Real Usage of Partial Classes in C#
Introduction
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.
Example
Fundamental Rules of Partial Class
- The partial modifier must be used just before the class keyword name.
- All the parts must be declared with a partial keyword, otherwise, it causes a compile-time error.
- Every part must be defined in the same namespace (assembly, dll, or exe).
- Each part must be available at compile time to form the final type.
- You can also have a constructor and destructor in a partial class.
- A partial class can have partial methods.
- All the parts must have the same accessibility (public, private, protected, etc).
- If any part is declared as abstract, then all the part is considered abstract.
- If any part is declared as sealed, then all the part is considered sealed.
- Inheritance between the partial parts is not allowed.
- The class member declared in a partial definition will be available to all the other parts.
- If any part declares a base type, then all the other parts also inherit that class automatically.
- All the parts can specify different base interfaces, but the final type implements all the interfaces listed by all the partial declarations.
- Nested partial types are allowed.
- All the parts can specify different class attributes (like SerializableAttribute, ObsoleteAttribute), but all the attributes are merged at compile time.
- At compile time XML-Comments of different parts will be merged.
Advantage of Partial Class
- With the help of partial classes, multiple developers can work simultaneously on the same class in different files.
- 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.
- You can also maintain and manage your application in an efficient manner by compressing large classes into small ones.
- 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
Web Form Application
Window Form Application
ADO.Net Entity Data Model
LINQ To SQL Class
Data Set Designer - XSD file (XML Schema Definition Language)
WPF User Controls
Live Example
Conclusion
- The partial keyword can be used to split the implementation of a class, an interface, a struct, a method in multiple source files.
- The partial types were introduced in C# 2.
- The partial modifier can only appear immediately before 'class', 'struct', 'interface', or 'void'.
- The partial keyword is not available for delegate and enum.
- Every subpart should be defined with a partial keyword.
- Every subpart should be defined in the same namespace.
Recommended Articles
Multiple Interface Inheritance in C# 8
Interface Virtual Method in C# 8
Interface Access Modifiers in C# 8
Interface Default Methods in C# 8
Thanks for visiting this page. Please follow us on Twitter, Facebook, LinkedIn, Youtube, Quora, and Pinterest for regular updates.
Video Recommendation
No comments:
Post a Comment
Please do not enter any HTML. JavaScript or spam link in the comment box.