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


当前回答

我注意到两个用法,我在答案中没有明确找到。

分类项目分组

一些开发人员使用注释来分离类的不同“部分”。例如,一个团队可能使用以下约定:

public class MyClass{  
  //Member variables
  //Constructors
  //Properties
  //Methods
}

对于分部类,我们可以更进一步,将部分分割到单独的文件中。按照惯例,团队可以在每个文件后面加上与之对应的部分。所以在上面我们会有这样的东西:MyClassMembers.cs, MyClassConstructors.cs, MyClassProperties.cs, MyClassMethods.cs。

正如其他答案所暗示的那样,是否值得将班级分开可能取决于在这种情况下班级的规模。如果它很小,那么把所有东西都放在一个大师班可能会更容易。但是,如果这些部分中的任何一部分太大,它的内容可以移动到一个单独的分部类中,以保持主类的整洁。在这种情况下,惯例可能是在节标题后留下类似“See partial class”的注释,例如:

//Methods - See partial class

管理使用语句/命名空间的范围

这种情况可能很少发生,但是来自您想要使用的库的两个函数之间可能会发生名称空间冲突。在单个类中,最多只能为其中一个使用using子句。对于另一种,您需要一个完全限定的名称或别名。对于部分类,由于每个名称空间& using语句列表都不同,因此可以将两组函数分离到两个单独的文件中。

其他回答

我看到的另一个用法是,

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

我有各种文件的名字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..
}

从MSDN:

1.在编译时,合并部分类型定义的属性。例如,考虑以下声明:

[SerializableAttribute]
partial class Moon { }

[ObsoleteAttribute]
partial class Moon { }

它们等价于以下声明:

[SerializableAttribute]
[ObsoleteAttribute]
class Moon { }

以下是所有部分类型定义的合并:

XML注释 接口 泛型类型参数属性 类属性 成员

2.另外,嵌套的分部类也可以是分部类:

partial class ClassWithNestedClass
{
    partial class NestedClass { }
}

partial class ClassWithNestedClass
{
    partial class NestedClass { }
}

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.

的确,Partial Class用于自动代码生成,其中一个用途是维护一个可能有上千行代码的大型类文件。你永远不知道你的类最终会有1万行,你不想用不同的名字创建一个新类。

public partial class Product
{
    // 50 business logic embedded in methods and properties..
}

public partial class Product
{
    // another 50 business logic embedded in methods and properties..
}
//finally compile with product.class file.

另一个可能的用途是多个开发人员可以使用同一个类,因为它们存储在不同的地方。人们可能会笑,但你永远不知道有时候情况会很棘手。

Product1.cs

public partial class Product
{
    //you are writing the business logic for fast moving product
}

Product2.cs

public partial class Product
{
    // Another developer writing some business logic...
}

希望这是有意义的!

我注意到两个用法,我在答案中没有明确找到。

分类项目分组

一些开发人员使用注释来分离类的不同“部分”。例如,一个团队可能使用以下约定:

public class MyClass{  
  //Member variables
  //Constructors
  //Properties
  //Methods
}

对于分部类,我们可以更进一步,将部分分割到单独的文件中。按照惯例,团队可以在每个文件后面加上与之对应的部分。所以在上面我们会有这样的东西:MyClassMembers.cs, MyClassConstructors.cs, MyClassProperties.cs, MyClassMethods.cs。

正如其他答案所暗示的那样,是否值得将班级分开可能取决于在这种情况下班级的规模。如果它很小,那么把所有东西都放在一个大师班可能会更容易。但是,如果这些部分中的任何一部分太大,它的内容可以移动到一个单独的分部类中,以保持主类的整洁。在这种情况下,惯例可能是在节标题后留下类似“See partial class”的注释,例如:

//Methods - See partial class

管理使用语句/命名空间的范围

这种情况可能很少发生,但是来自您想要使用的库的两个函数之间可能会发生名称空间冲突。在单个类中,最多只能为其中一个使用using子句。对于另一种,您需要一个完全限定的名称或别名。对于部分类,由于每个名称空间& using语句列表都不同,因此可以将两组函数分离到两个单独的文件中。