Detailed explanation of scope examples of five access modifiers in C#

零下一度
Release: 2017-06-29 14:36:23
Original
4806 people have browsed it

In the C# language, there are five access modifiers: public, private, protected, internal, and protected internal. The scope of scope is as follows:
Access modifier Description
public Public access. without any restrictions.
private Private access. Access is limited to members of this class, not subclasses or instances.
protected Protected access. Access is limited to this class and subclasses, and instances cannot be accessed.
internal Internal access. Access is limited to this project and cannot be accessed by others.
protected internal Internal protected access. Access is limited to this project or subclasses. Other inaccessible
C# member types can be modified and default modifiers are as follows:
Member type Default modifier Can be modified
enum public none
class private public, protected, internal, private,
protected internal
interface public none
struct private public, internal, private
Now I will talk about public, private, protected, internal and The scope of protected internal.
The following code:

[csharp] view plain copy
 
using System;  
using System.Collections.Generic;  
using System.Text;  
  
namespace AccessModifier  
{  
    public class AccessModifierClass  
    {  
        public string GetPublicString()  
         {  
            return "Public String";  
         }  
  
        protected string GetProtectedString()  
         {  
            return "Protected String";  
         }  
  
         private string GetPrivateString()  
         {  
            return "Private String";  
         }  
  
         internal string GetInternalString()  
         {  
            return "Internal String";  
         }  
  
        protected internal string GetProtectedInternalString()  
         {  
            return "Protected Internal String";  
         }  
  
        void AvailableAccessModifier()  
        {  
         this.GetPublicString();  
         this.GetPrivateString();  
         this.GetInternalString();  
         this.GetProtectedInternalString();  
         this.GetProtectedString();  
        }  
     }  
  
  
    public class TestAccessModifierClass1  
     {  
         void AvailableAccessModifier()  
         {  
             AccessModifierClass item = new AccessModifierClass();  
             item.GetPublicString();  
             item.GetInternalString();  
             item.GetProtectedInternalString();  
         }  
     }  
  
     public class TestAccessModifierClass2 : AccessModifierClass  
     {  
         void AvailableAccessModifier()  
         {  
             AccessModifierClass item = new AccessModifierClass();  
             item.GetPublicString();  
             item.GetInternalString();  
             item.GetProtectedInternalString();  
             base.GetProtectedString();  
         }  
     }  
 }
Copy after login


AccessModifierClass is our access modifier class, which has five access modifier methods. It can be seen that the AvailableAccessModifier() method in the AccessModifierClass class can access all Methods.
The "AvailableAccessModifier()" method in the "TestAccessModifierClass1" class can only access the "public, Internal" and "Protected Internal" methods.

The TestAccessModifierClass2 class inherits from the AccessModifierClass class, so its AvailableAccessModifier() method can access the public, internal, protected and protected internal methods.

The above is the detailed content of Detailed explanation of scope examples of five access modifiers in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!