我想知道是否有人能给我一个概述,为什么我要使用它们,在这个过程中我能获得什么好处。


当前回答

我发现令人不安的是,“凝聚力”这个词在这些帖子中没有出现(直到现在)。 我也很不安,有人认为启用或鼓励大型类和方法在某种程度上是件好事。 如果你试图理解和维护代码基础的“部分”,那就糟透了。

其他回答

大多数人认为partial只应该用于生成了代码文件的类或接口。我不同意,原因如下。

举个例子,让我们看看c#系统。数学课…这是类。我不会尝试把70多个方法都塞进同一个代码文件中。维持它将是一场噩梦。

将每个数学方法放入单独的部分类文件中,并将所有代码文件放入项目中的math文件夹中,将显著地使组织更加干净。

同样的道理也适用于许多其他具有大量不同功能的类。例如,管理PrivateProfile API的类可以通过在单个项目文件夹中拆分为一组干净的部分类文件而受益。

就我个人而言,我还将大多数人所说的“助手”或“实用工具”类划分为每个方法或方法功能组的单独部分文件。例如,在一个项目中,字符串助手类几乎有50个方法。即使使用区域,这也将是一个冗长而笨拙的代码文件。为每个方法使用单独的部分类文件维护要容易得多。

我只会小心使用部分类,并在整个项目中保持所有代码文件布局一致。例如,将任何类公共枚举和类私有成员放入文件夹中的Common.cs或类似名称的文件中,而不是将它们分散到文件中,除非它们只特定于包含它们的部分文件。

请记住,当您将一个类拆分为单独的文件时,您也失去了使用文本编辑器拆分栏的能力,该功能可以让您同时查看当前文件的两个不同部分。

Multiple Developer Using Partial Classes multiple developer can work on the same class easily. Code Generator Partial classes are mainly used by code generator to keep different concerns separate Partial Methods Using Partial Classes you can also define Partial methods as well where a developer can simply define the method and the other developer can implement that. Partial Method Declaration only Even the code get compiled with method declaration only and if the implementation of the method isn't present compiler can safely remove that piece of code and no compile time error will occur. To verify point 4. Just create a winform project and include this line after the Form1 Constructor and try to compile the code partial void Ontest(string s);

以下是在实现分部类时需要考虑的几点

Use partial keyword in each part of partial class. The name of each part of partial class should be the same but the source file name for each part of partial class can be different. All parts of a partial class should be in the same namespace. Each part of a partial class should be in the same assembly or DLL, in other words you can't create a partial class in source files from a different class library project. Each part of a partial class must have the same accessibility. (i.e: private, public or protected) If you inherit a class or interface on a partial class then it is inherited by all parts of that partial class. If a part of a partial class is sealed then the entire class will be sealed. If a part of partial class is abstract then the entire class will be considered an abstract class.

作为预编译器指令的替代方案。

如果您使用预编译器指令(即# If DEBUG),那么您最终会看到一些看起来粗糙的代码与实际的Release代码混合在一起。

你可以创建一个单独的分部类来包含这些代码,或者将整个分部类包装在一个指令中,或者省略该代码文件,使其不被发送给编译器(实际上是做同样的事情)。

除了其他答案之外……

我发现它们可以作为重构神类的垫脚石。如果一个类有多个职责(特别是如果它是一个非常大的代码文件),那么我发现为每个职责添加1x个部分类作为组织和重构代码的第一步是有益的。

这非常有帮助,因为它可以帮助使代码更具可读性,而不会实际影响执行行为。它还可以帮助确定何时易于重构某个职责,或者何时与其他方面紧密纠缠在一起。

然而,需要明确的是,这仍然是糟糕的代码,在开发结束时,您仍然希望每个类有一个职责(而不是每个分部类)。这只是一块垫脚石:)

我看到的另一个用法是,

扩展一个关于数据访问逻辑的大型抽象类,

我有各种文件的名字Post.cs,Comment.cs,Pages.cs…

in Post.cs 

public partial class XMLDAO :BigAbstractClass
{
// CRUD methods of post..
}


in Comment.cs 

public partial class XMLDAO :BigAbstractClass
{
// CRUD methods of comment..
}

in Pages.cs 

public partial class XMLDAO :BigAbstractClass
{
// CRUD methods of Pages..
}