C# - “部分”的好处是什么?课程?
我问这个问题是因为我发现分发类定义是一个非常危险的功能,这样您就无法真正确定您是否了解所有信息。即使我找到了三个部分定义,我怎么知道某个地方没有第四个?
我是 C# 新手,但已经使用 C++ 10 年了,也许这就是我感到震惊的原因?
无论如何,“部分”概念一定有一些很大的好处,而我显然缺少这一点。我很想更多地了解其背后的哲学。
编辑:抱歉,错过了此重复项搜索现有帖子时。
I'm asking this because I find it quite a dangerous feature to distribute the class definition so that you can't really be sure if you know all about it. Even if I find three partial definitions, how do I know that there's not a fourth somewhere?
I'm new to C# but have spent 10 years with C++, maybe that's why I'm shaken up?
Anyway, the "partial" concept must have some great benefit, which I'm obviously missing. I would love to learn more about the philosophy behind it.
EDIT: Sorry, missed this duplicate when searching for existing posts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用代码生成时,分部类非常方便。如果您想要修改生成的类(而不是继承它),那么在重新生成代码时您将面临丢失更改的风险。如果您能够在单独的文件中定义额外的方法等,则可以重新创建类的生成部分,而无需破坏您手工编写的代码。
Partial classes are handy when using code generation. If you want to modify a generated class (rather than inheriting from it) then you run the risk of losing your changes when the code is regenerated. If you are able to define your extra methods etc in a separate file, the generated parts of the class can be re-created without nuking your hand-crafted code.
最大的好处是隐藏计算机生成的代码(由设计者)。
Eric Lippert 最近发表了一篇关于 部分关键字 一般来说。
另一种用法是为嵌套类提供自己的文件。
The great benefit is to hide computer generated code (by the designer).
Eric Lippert has a recent blog post about the partial-keyword in general.
Another usage could be to give nested classes their own file.
还有一点是,当一个类实现多个接口时,可以将接口实现拆分到不同的文件中。
所以每个代码文件都只有属于接口实现的代码。它符合关注点分离概念。
An other point is, that when a class implements multiple interfaces, you can split the interface implementations on diffrent files.
So every code file has only the code that belongs to the interface implementation. It´s according to separation of concerns concept.
两个人编辑同一个类,自动生成的设计器代码是我可以看到的两个直接功能,它们是通过部分类和方法解决的。
与 1.1 相比,让设计者在单独的文件中生成代码要容易得多,在 1.1 中,您的代码经常会被 Visual Studio(在 Windows 窗体中)破坏。
Visual Studio 仍然在将设计器文件、隐藏代码和设计文件与 ASP.NET 同步方面搞得一团糟。
Two people editing the same class, and autogenerated designer code are the two immediate features I can see that were solved by partial classes and methods.
Having designer generated code in a separate file is a lot easier to work with compared to 1.1, where you code could often be mangled by Visual Studio (in windows forms).
Visual Studio still makes a mess of syncing the designer file, code behind and design file with ASP.NET.
如果您有某种大得离谱的类,由于某种原因无法或不允许在逻辑上分解为较小的类,那么您至少可以在物理上将它们分解为多个文件,以便更有效地使用它。本质上,您可以一次查看小块,避免上下滚动。
这可能适用于遗留代码,由于存在大量根深蒂固的依赖关系,这些代码可能由于某些神秘的策略而不允许与现有 API 发生冲突。
不一定是部分类的最佳使用,但肯定为您提供了组织代码的替代选项,您可能无法以其他方式修改。
If you have some kind of absurdly large class that for some reason are unable or not allowed to logically break apart into smaller classes then you can at least physically break them into multiple files in order to work with it more effectively. Essentially, you can view small chunks at a time avoiding scrolling up and down.
This might apply to legacy code that perhaps due to some arcane policy are not allowed to mess with the existing API because of numerous and entrenched dependencies.
Not necessarily the best use of partial classes, but certainly gives you an alternate option to organize code you might not be able to otherwise modify.
也许已经太晚了,但请让我也添加我的 2 美分:
*。在处理大型项目时,将一个类分布在单独的文件中允许多个程序员同时处理它。
*.您可以轻松地为 VS.NET 生成的类编写代码(用于扩展功能)。这将允许您编写自己需要的代码,而不会弄乱系统生成的代码
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