根据 MSDN Documentation对于部分类(class):
All the parts must have the same accessibility, such as public, private, and so on.
但是如果您创建一个 WindowsForm 应用程序,您将在两个分部类中拥有默认的 Form 类。
背后的代码:
public partial class Form1 : Form
{
...
}
和设计师:
partial class Form1
{
...
}
访问修饰符不同,但它会编译。
我是不是漏掉了什么?
最佳答案
如果您没有在分部类的一部分中指定访问修饰符,它会使用与另一部分相同的访问修饰符。
C# 5 规范的相关部分:§10.2.2
When a partial type declaration includes an accessibility specification (the public, protected, internal, and private modifiers) it must agree with all other parts that include an accessibility specification. If no part of a partial type includes an accessibility specification, the type is given the appropriate default accessibility (§3.5.1).
因此规范说可访问性必须与其他部分一致如果指定;换句话说,不必在每个部分都指定它。措辞可能会更改为不那么含糊,但是...
关于c# - 部分类和访问修饰符问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25399531/