Search code examples
c#partial-classes

Under what conditions it is good to have a "partial class?"


When does it become a good idea to have your class separate into two .cs and have it as a partial class?

Are there some signs showing that it is time to go with partial class?

Thanks!


Solution

  • When you have to autogenerate a portion of those classes and manually write the rest of the content of the classes.

    This is so that you can put the machine-generated content in one file and hand-coded code in another file. The advantage of doing so is that when you have to regenerate the source code, your hand-coded portion won't get wiped out.

    This is how MS generates class content for its GUI designers ( think of those *.designer.cs file), and allows you to put the meat of your logic in other related file ( *.cs)