C#:嵌套类的构造函数使 "inaccessible due to protection level"

标签 c# .net class oop

我有这段代码,并产生了错误,只是因为我已经添加到它的类的构造函数中。

class NestedClass
{
   class A
   {
      A() {}
   }

   class B
   {
       // no constructor
   }

   public static void run()
   {
     A a = new A();  // error
     B b = new B(); // no error
   }
}

错误是:

NestedExample.A is inaccessible due to protection level

请帮我解释一下。

谢谢:)

最佳答案

您的构造函数是私有(private)的。类成员的默认访问修饰符是 private

   class A
   {
      A() {}
   }

这是正确的实现

   class A
   {
      public A() {}
   }

关于C#:嵌套类的构造函数使 "inaccessible due to protection level",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14068174/

相关文章:

c# - 将对象转换为枚举 C#

c# - WPF DataGrid 绑定(bind)到 IEnumerable<KeyValuePair<int, CustomType>>

c# - 是否有类似 boost::asio for .NET (Mono) 的东西?

javascript - jquery复选框元素类click

java - 如何将项目从netbeans中的另一个类添加到列表框

c# - 将点符号插入字符串数字

c# - C# datagridview 中的存储过程而不是列表框

c# - RedirectStandardOutput 和批处理文件中的暂停命令

c# - Log4net 停止使用自定义渲染器

javascript - 内置类的子类的私有(private)字段在被覆盖的方法中是否不可访问?