开发者

C# - To use Partial Classes, or Seperate Ones? [duplicate]

开发者 https://www.devze.com 2022-12-08 12:28 出处:网络
This question already has answers here: Closed 10 years ago. Possible Duplicate: Why use partial classes?
This question already has answers here: Closed 10 years ago.

Possible Duplicate:

Why use partial classes?

Which way is best practice and the best programming approach, Partial Classes or Individual cla开发者_StackOverflow中文版sses?


Only use partial classes where there's a clear benefit in separating out the logic for one class into multiple files. The most obvious example is where some part of the code is autogenerated - code generators should usually build partial types, to allow for them to be augmented by the developer.

They can also be useful when refactoring one class into two, to get a feel of what the classes will look like afterwards.

Those are the major use cases really - I wouldn't start using them too liberally; it makes it harder to work out where everything is. Obviously there are exceptions... for example the System.Linq.Enumerable class in .NET 3.5 must be huge, and it would make sense to implement that with partial types to end up with manageable files.


Partial classes are great for the differentiation between the GUI interface that is auto-generated, and the partial class that corresponds to that for YOUR code makes sure you and code which is auto-generated don't step on each other's toes. Take advantage of that.

As for building your own custom classes / GUI controls, I would stay the same way. Let the IDE help with the visual cosmetics, and the rest of the partial class to perform your custom functionality.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号