c# - 部分类和访问修饰符问题

标签 c# .net access-modifiers partial-classes

根据 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/

相关文章:

c# - 其他项目中缺少 MySQL 连接字符串

c# - 使用键值数组访问字典

C#。如果 "after await"线程忙,会发生什么?

.net - Azure WebJob 中缺少 dotnet.exe

c# - 如何使用 SQL CE 加速 LINQ 插入?

c# - EF Core 中返回多个结果集的映射存储过程

c# - 两个相等性检查在一个 Where 中,反之亦然以提高效率

.net - 什么时候应该 [程序集 : InternalsVisibleTo()] be used?

java - 私有(private)、 protected 访问修饰符和抽象之间的区别

c# - 为什么C#禁止接口(interface)成员的内部访问?