Partial Classes in C#

2019-04-04 10:16发布

Are there are good uses of Partial Classes outside the webforms/winforms generated code scenarios? Or is this feature basically to support that?

20条回答
地球回转人心会变
2楼-- · 2019-04-04 10:27

I use partial classes as a means of separating out the different sub elements of custom controls that I write. Also, when used with entity creation software, it allows products like LLBLGen to create generated versions of classes, as well as a custom, user edited version, that won't get replaced if the entities need to be regenerated.

查看更多
ら.Afraid
3楼-- · 2019-04-04 10:27

maybe its too late but please let me to add my 2 cents too:

*.When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.

*.You can easily write your code (for extended functionality) for a VS.NET generated class. This will allow you to write the code of your own need without messing with the system generated code

查看更多
The star\"
4楼-- · 2019-04-04 10:27

Sometimes you might find terribly old code at work that may make it close to impossible to refactor out into distinct elements without breaking existing code.

When you aren't given the option or the time to create a more genuine architecture, partial classes make it incredibly easy to separate logic where its needed. This allows existing code to continue using the same architecture while you gain a step closer to a more concrete architecture.

查看更多
乱世女痞
5楼-- · 2019-04-04 10:33

It is in part to support scenarios (WebForms, WinForms, LINQ-to-SQL, etc) mixing generated code with programmer code.

There are more reasons to use it. For example, if you have big classes in large, unwieldy files, but the classes have groups of logically related methods, partial classes may be an option to make your file sizes more manageable.

查看更多
Ridiculous、
6楼-- · 2019-04-04 10:34

I often use partial classes to give each nested class its own file. There have been some architectures I've worked on where most of the implementation was only required by one class and so we nested those classes in that one class. It made sense to keep the files easier to maintain by using the partial class ability and splitting each one into its own file.

We've also used them for grouping stock overrides or the hiding of a stock set of properties. Things like that. It's a handy way of mixing in a stock change (just copy the file and change the partial class name to the target class - as long as the target class is made partial too, of course).

查看更多
女痞
7楼-- · 2019-04-04 10:35

As mentioned earlier, I too think this is a code smell.

If a class is so big that it needs to be split into more files, means that it is breaking the single responsibility principle and doing too many things. The large class could be broken down into smaller classes that cooperate together.

If you have to use partial classes or regions to organize code, consider if they should be in their own classes. It increases readability and you'd get more code reuse.

查看更多
登录 后发表回答