Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中,访问说明符保护和内部保护有什么区别#_C#_Access Specifier - Fatal编程技术网

C# 在C中,访问说明符保护和内部保护有什么区别#

C# 在C中,访问说明符保护和内部保护有什么区别#,c#,access-specifier,C#,Access Specifier,在C#中访问说明符受保护和内部受保护之间有什么区别?内部可以在程序集中看到 namespace InternalTest ----This namespace in assembly One { Public class A { B ol=new B(); Console.WriteLine(ol.x);//Output:5 Console.WriteLine(ol.y);//error will occured. Bec

在C#中访问说明符
受保护
内部受保护
之间有什么区别?

内部
可以在程序集中看到

 namespace InternalTest   ----This namespace in assembly One
 {
    Public class A
    {
       B ol=new B();
        Console.WriteLine(ol.x);//Output:5  
        Console.WriteLine(ol.y);//error will occured. Because protected is like Private variable
    }

    public class B
     {
        Internal int x=5;
         Protected int y=5;

      }
 }
     using InternalTest;   
     namespace InternalTest1   ----This namespace in assembly Two
     {
       Public class A1:B
        {
           Public void GetInternalValue()
          {                     return x; //error can't access because this is internal

          }

          Public void GetProtectedValue()
          {
                return y;//Work because it's protected

          }
      }
       public class C
       {


       }

    }
Protected
可以通过从定义它的类继承的类来查看

受保护的内部
可以在程序集中或从定义它的类派生的类型(包括来自其他程序集中的类型)中看到

见:

从页面复制:

public              Access is not restricted.
protected           Access is limited to the containing class or types derived from the containing class.
internal            Access is limited to the current assembly.
protected internal  Access is limited to the current assembly or types derived from the containing class.
private             Access is limited to the containing type.

internal protected
允许您从不是从同一对象派生的类访问同一程序集中的成员,但也允许您从另一个程序集中访问成员时获得的标准受保护访问权限。它是内部受保护的,而不是内部受保护的(尽管CLR允许后者,C不允许)

内部受保护的
内部受保护的
,这与外部受保护的(从当前程序集外部)和内部受保护的(从同一程序集中)的意思相同.

受保护
表示只有当前类及其派生的任何类可以访问该成员

internal
表示当前程序集中的任何类都可以访问该成员

protectedinternal
本质上是指
protected
internal
;i、 例如,与当前程序集中的所有类一样,从当前类派生的所有类(在任何程序集中)都可以访问该成员。这与许多开发人员所期望的相反,
protectedinternal
的意思与
protected
internal
的意思相同(事实并非如此)。

  • internal
    -由同一程序集(.dll或.exe)中的任何内容可见
  • 受保护
    -任何子类都可见,无论它们位于何处
  • 内部保护
    -由同一程序集和任何子类中的任何内容可见,无论它们位于何处

杰夫·马特菲尔德(Jeff Mattfield)说“内部进一步降低了可视性”的方式让人不清楚<代码>内部和受保护是完全不同的可视性。将两者结合在一起可以使成员更加可见。没有显式访问修饰符的对象的默认可见性尽可能小。添加任何访问修饰符都会增加可见性。

受保护的内部受保护的访问说明符与继承的概念有关

让我们举例说明受保护的受保护的内部

有两个名为命名空间A命名空间B的命名空间

名称空间A中,有一个名为classA的类,该类由一个名为accept()的方法组成,该方法使用受保护的访问说明符

命名空间B中,有另一个名为classB的类,它继承自命名空间A的classA

现在,在这个受保护的说明符的帮助下,我们可以访问命名空间BclassB中的accept()方法

但当使用受保护的内部访问说明符时,这一概念并不正确:
如果命名空间AclassA函数使用了受保护的内部访问说明符,则命名空间B的classB无法访问它,因为接受()函数只能在同一命名空间内的继承类中访问。

要更好地理解受保护和受保护内部之间的区别,最好先了解受保护和内部之间的区别

内部变量引用同一程序集。Yo无法在不同的程序集中访问。 受保护变量类似于私有变量,但您可以在同一程序集或不同程序集中的驱动类中访问

 namespace InternalTest   ----This namespace in assembly One
 {
    Public class A
    {
       B ol=new B();
        Console.WriteLine(ol.x);//Output:5  
        Console.WriteLine(ol.y);//error will occured. Because protected is like Private variable
    }

    public class B
     {
        Internal int x=5;
         Protected int y=5;

      }
 }
     using InternalTest;   
     namespace InternalTest1   ----This namespace in assembly Two
     {
       Public class A1:B
        {
           Public void GetInternalValue()
          {                     return x; //error can't access because this is internal

          }

          Public void GetProtectedValue()
          {
                return y;//Work because it's protected

          }
      }
       public class C
       {


       }

    }
2) 采取不同的装配方式

 namespace InternalTest   ----This namespace in assembly One
 {
    Public class A
    {
       B ol=new B();
        Console.WriteLine(ol.x);//Output:5  
        Console.WriteLine(ol.y);//error will occured. Because protected is like Private variable
    }

    public class B
     {
        Internal int x=5;
         Protected int y=5;

      }
 }
     using InternalTest;   
     namespace InternalTest1   ----This namespace in assembly Two
     {
       Public class A1:B
        {
           Public void GetInternalValue()
          {                     return x; //error can't access because this is internal

          }

          Public void GetProtectedValue()
          {
                return y;//Work because it's protected

          }
      }
       public class C
       {


       }

    }
从上面的示例中可以清楚地看到,您可以在同一个程序集中访问内部变量,但不能在不同的程序集中访问。您可以说,在同一个程序集中,它看起来像公共变量。您可以通过创建类的对象来赋值

3) 受保护的内部变量在同一程序集中具有这两者的优点,它看起来像公共变量。
在diifrent程序集中,您使用like受保护变量

受保护

如果该类声明为“受保护”,则表示该类可以由程序集中的子类访问,也可以由程序集中外部的子类访问

 namespace InternalTest   ----This namespace in assembly One
 {
    Public class A
    {
       B ol=new B();
        Console.WriteLine(ol.x);//Output:5  
        Console.WriteLine(ol.y);//error will occured. Because protected is like Private variable
    }

    public class B
     {
        Internal int x=5;
         Protected int y=5;

      }
 }
     using InternalTest;   
     namespace InternalTest1   ----This namespace in assembly Two
     {
       Public class A1:B
        {
           Public void GetInternalValue()
          {                     return x; //error can't access because this is internal

          }

          Public void GetProtectedValue()
          {
                return y;//Work because it's protected

          }
      }
       public class C
       {


       }

    }
内部

如果类被声明为“internal”,则表示程序集中的任何类都可以访问它

 namespace InternalTest   ----This namespace in assembly One
 {
    Public class A
    {
       B ol=new B();
        Console.WriteLine(ol.x);//Output:5  
        Console.WriteLine(ol.y);//error will occured. Because protected is like Private variable
    }

    public class B
     {
        Internal int x=5;
         Protected int y=5;

      }
 }
     using InternalTest;   
     namespace InternalTest1   ----This namespace in assembly Two
     {
       Public class A1:B
        {
           Public void GetInternalValue()
          {                     return x; //error can't access because this is internal

          }

          Public void GetProtectedValue()
          {
                return y;//Work because it's protected

          }
      }
       public class C
       {


       }

    }
受保护的内部


如果该类声明为“受保护的内部”,则表示只能通过其派生类在程序集中访问该类。

-更新答案2019-

您好,您可以通过下表找到辅助功能


内部保护的可能重复以及保护的意义是什么?如果它是
受保护的内部
,并且您从另一个程序集中的类派生,您将无法访问该方法等。但是如果它只是
受保护的
,您将可以。如果我无法访问另一个程序集中的方法,然后我可以简单地将其标记为内部,而不是受保护的内部。我是不是遗漏了什么@Vince嗯,是的,但是你可以访问这个方法,而不必从它派生。你说“如果它是受内部保护的,并且你从另一个程序集中的类派生,