What is the utility of partial class
, and in what situations is it recommended to use them? I have some classes in a project that are partial classes
and I suspect a supposed problem is related to this.
What is the utility of partial class
, and in what situations is it recommended to use them? I have some classes in a project that are partial classes
and I suspect a supposed problem is related to this.
Partial classes are usually used with code generated by some tool, in order to allow the programmer to inject code within that generated class, without having to change the generated code file.
Apart from this utility, I would say that using partial classes in an unprocessed code makes the code confusing. Some have already tried to convince me that this could be used to better organize the functions of a class, but there are coding standards for this.
The principle SOLID for example, prohibits the same class from having several functions ... so you would not have to separate the code from one class into multiple parts on an organization account.
Although often referred to as partial classes (or partial types), this is in fact a partial definition that is governed by section §10.2 of the specification.
It is generally used by generated code and allows the developer to add or modify, in conjunction with partial methods (section §10.2.7 of the specification), the type generated.
It is also very useful, for example, to separate the implementation of one type into several files.
For example, if you need to implement the IListz<T>
interface in a class this implies implementing a number of interfaces that IList<T>
extends. Placing the implementation of each interface in its own partial definition file reduces the size of each file making it easier to maintain. When you look at the version control history it is also easier to see what part of the class has been modified.