C# 为什么不能显式声明命名空间中定义的元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9158037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:11:52  来源:igfitidea点击:

Why elements defined in a namespace cannot be explicitly declared?

c#access-modifiers

提问by Michael

I have the following C# code:

我有以下 C# 代码:

namespace ISeeOptic.BL
{

  public abstract class Process
  {        
     ...      

     protected static void DeleteImages(List<ImagesPath> list)
      {
          some logic
      } 

      ...
   }


    protected class GetDataBL: Process
    {
      ...

     public static void DeleteImages(List<ImagesPath> list)
     {
         DeleteImages(list); 
     } 
     ...
 }
} 

At compile-time I get the following Error:

在编译时,我收到以下错误:

Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal

命名空间中定义的元素不能显式声明为私有、受保护或受保护的内部

I'm beginner in C# so maybe this question may seem naive, any idea what cause to this error?

我是 C# 初学者,所以这个问题可能看起来很幼稚,知道这个错误的原因是什么吗?

Thank you advance.

先谢谢了。

采纳答案by phoog

Elements defined in a namespace may be explicitly declared public or internal.

命名空间中定义的元素可以显式声明为 public 或 internal。

They may not be explicitly declared private or protected (or protected internal) because these modifiers only make sense for members of a class.

它们可能不会被显式声明为私有或受保护(或受保护的内部),因为这些修饰符仅对类的成员有意义。

Your protected class GetDataBL, for example, makes no sense, because "protected" means "accessible to classes that inherit from the containing class" -- but there is no containing class for GetDataBL.

protected class GetDataBL例如,您的没有任何意义,因为“受保护”的意思是“可访问从包含类继承的类”——但 GetDataBL 没有包含类。

回答by Jon Skeet

(I believe you'll actually get a compile-time error; if you're only seeingthis at execution time, then chances are your code is being compiled at execution time too, e.g. as part of a web app. Logically it's a compile-time error, not an exception.)

(我相信你实际上会得到一个编译时错误;如果你只在执行时看到这个,那么你的代码很可能也在执行时被编译,例如作为 web 应用程序的一部分。逻辑上它是一个编译-时间错误,不是例外。)

The protectedaccess modifier (loosely) makes a member accessible to a derived containing type; but in the case of a namespace member there isno containing type.

protected访问修饰符(松散地)作出到派生包含类型访问的构件; 但在一个空间成员的情况下,存在不包含类型。

Likewise a private member's accessibility domain is the program text of the containing type - and again, there isno containing type.

同样的私有成员的访问域是包含类型的程序文本-又一次,也就是不包含类型。

What are you actually trying to achieveby making GetDataBLprotected?

你实际上想要通过保护来实现GetDataBL什么?

回答by gdoron is supporting Monica

privateprotectedmeans they will be accessible to this class or to the derived classes.
In the Namespace level there is no class to derived from so it useless.

privateprotected意味着它们可以被这个类或派生类访问。
在命名空间级别没有派生自的类,所以它没用。

You can use only publicor internalin the Namespacelevel

您可以只使用publicinternalNamespace级别

MSDN docs

MSDN 文档

回答by Grant Thomas

It's the scoping of the elements that is causing the error, as explained by the error - and the C# specification (ECMA section 10.5.1):

正如错误所解释的,这是导致错误的元素的范围 - 和 C# 规范(ECMA 第 10.5.1 节):

  • Types declared in compilation units or namespaces can have publicor internaldeclared accessibility and default to internaldeclared accessibility.
  • Class members can have any of the five kinds of declared accessibility and default to privatedeclared accessibility.
  • Struct members can have public, internal, or privatedeclared accessibility and default to privatedeclared accessibility because structs are implicitly sealed.
  • 在编译单元或命名空间中声明的类型可以具有publicinternal声明可访问性,并且默认为internal声明的可访问性。
  • 类成员可以具有五种声明的可访问性中的任何一种,并且默认为private声明的可访问性。
  • 结构成员可以具有publicinternalprivate声明的可访问性,并且默认为private声明的可访问性,因为结构是隐式密封的。

回答by Afazal

In class Private, Protected and Protected Internal access Specifiers are not allowed at namespace level.

在名称空间级别不允许使用 Private、Protected 和 Protected Internal 访问说明符。

Only allowed Specifiers are public and internal in class.

只有允许的说明符在类中是公共的和内部的。

Only to the child classes private, protected or protected internal access Specifiers are allowed .

仅允许对子类私有、受保护或受保护的内部访问说明符。

Sample code

示例代码

internal class ParentClass
{
    public string test()
    {
        return "This is the parent class function";
    }
    private class BaseChildClass
    {
        protected string childtest()
        {
            return "This is the parent class function";
        }
    }

    private class DerivedChildClass : BaseChildClass
    {
        private void test1()
        {
            string test = base.childtest();

        }
    }
}