1
What is the use of a partial class
, and in which situations is it recommended to use them? I have some classes in a project that are partial classes
and I suspect that an alleged problem is related to this.
1
What is the use of a partial class
, and in which situations is it recommended to use them? I have some classes in a project that are partial classes
and I suspect that an alleged problem is related to this.
4
Partial classes are usually used with code generated by some tool, in order to allow the programmer to input 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 unmanaged code causes the code to get confused. Some have 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... thus it would not be necessary to separate the code of the same class into several parts on account of organization.
2
Although often referred to as partial classes (or partial types), it is actually a partial definition that is governed by section §10.2 of the specification.
It is usually 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 generated type.
It is also very useful, for example, to separate the implementation of one type into several files.
For example, if it is necessary to implement the interaction IListz<T>
in a class this implies implementing a number of interfaces that IList<T>
extend. Placing the implementation of each interface in its own partial definition file reduces the size of each file making it easier to maintain it. When analyzing version control history it is also easier to see which part of the class has been modified.
Browser other questions tagged c#
You are not signed in. Login or sign up in order to post.
And what’s the problem?
– Oralista de Sistemas
For sure, it is not the partial class that will generate problems, it is a notation for the same class to stay in different files.
– Maurício Júnior