Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Can you explain to me when there would be a legitimate reason to ever use a partial class?</p> </blockquote> <p>One of the most legitimate and useful reasons is to encourage the separation of automatically generated code and your own custom extensions to it. For instance, it's common to have an automatically generated form code from some kind of designer, but you usually want to add your own specific behavior to it. This way, if you regenerate the automatic-code portion, you're not touching the part that has your specific extensions.</p> <p>That said, it's quite possible to have too much of a good thing. Some tips:</p> <ul> <li><p>Don't make your classes <code>partial</code> for the sake of being <code>partial</code>.</p></li> <li><p>Don't put partial classes anywhere except besides one another. If you have to jump to a completely unrelated section of the project to see the other half of the class, you're probably doing it wrong.</p></li> <li><p>Don't use <code>partial</code> as a technique to obscure the size of the class. If you're breaking up your classes with <code>partial</code> because they're too big, you should revisit the <strong><a href="http://en.wikipedia.org/wiki/Single_responsibility_principle" rel="noreferrer">Single Responsibility Principle</a></strong>.</p></li> <li><p>If you have three or more <code>partial</code> fragments for the same class, it's almost a guarantee that you're abusing partial. Two is the typical upper bound of reasonableness, and it's generally used to segment automatically-generated code from handwritten code.</p></li> </ul> <blockquote> <p>Anyway, long story short - he uses partial classes, all over the place, as a way to fake multiple inheritance in C# (IMO). Why he didnt just split the classes up into multiple ones and use composition is beyond me. He will have 3 'partial class' files to make up his base class, each w/ 3-500 lines of code... And does this several times in his API.</p> </blockquote> <p>Yes, that's definitely a clear abuse of <code>partial</code>!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload