Partial Classes

Added in .NET 2.0
A partial class allows you to split a class into separate source files
Prior to C# 2.0 you had to define each class entirely in a single file
Automatically generated code can now live in a different file which prevents this code from being accidentally modified


This allows you to split the definition across two or more files
There are a number of situations where splitting a class definition makes sense:
1 - when working on large projects this allows multiple developers to create a single class
2 - automatically generated source code can be added without recreating the source file. This is used for windows forms, web service wrappers etc


To split a class use the partial keyword modifier
All the parts must have the same accessibility (public or private)



For example the hidden classes behind Document VSTO solutions use partial classes as a way of seperating the auto-generated code from the code you write.
Partial classes are also used in Windows forms to separate the auto-generated code from the code you write.


The Partial Keyword is not needed for the main class definition, it is needed only for additional class definitions that share the same class name.
When you compile the code, the code is automatically merged together.


You can also use partial classes to divide a programming task between 2 developers.


You can also define partial interfaces and structures.


Uses the Partial keyword






© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext