Home Backend Development C#.Net Tutorial Detailed explanation of scope examples of five access modifiers in C#

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

Jun 29, 2017 pm 02:36 PM
.net private protected

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

The development of artificial intelligence (AI) technologies is in full swing today, and they have shown great potential and influence in various fields. Today Dayao will share with you 4 .NET open source AI model LLM related project frameworks, hoping to provide you with some reference. https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel is an open source software development kit (SDK) designed to integrate large language models (LLM) such as OpenAI, Azure

What are the employment prospects of C#? What are the employment prospects of C#? Oct 19, 2023 am 11:02 AM

Whether you are a beginner or an experienced professional, mastering C# will pave the way for your career.

What does private mean in java What does private mean in java Nov 24, 2022 pm 06:27 PM

In Java, private means "private" and is an access control modifier used to modify classes, properties and methods. Class members modified with private can only be accessed and modified by the methods of the class itself, and cannot be accessed and referenced by any other class (including subclasses of the class); therefore, the private modifier has the highest level of protection.

.NET performance optimization technology for developers .NET performance optimization technology for developers Sep 12, 2023 am 10:43 AM

If you are a .NET developer, you must be aware of the importance of optimizing functionality and performance in delivering high-quality software. By making expert use of the provided resources and reducing website load times, you not only create a pleasant experience for your users but also reduce infrastructure costs.

Performance differences between Java framework and .NET framework Performance differences between Java framework and .NET framework Jun 03, 2024 am 09:19 AM

In terms of high-concurrency request processing, .NETASP.NETCoreWebAPI performs better than JavaSpringMVC. The reasons include: AOT early compilation, which reduces startup time; more refined memory management, where developers are responsible for allocating and releasing object memory.

Detailed explanation of private access modifiers for Java functions Detailed explanation of private access modifiers for Java functions Apr 25, 2024 pm 04:48 PM

Private is a Java access modifier that limits the accessibility of a function to only the class in which it is defined, including: the function cannot be accessed in other classes. The function is also not accessible in subclasses.

In Java, can we declare a top-level class as protected or private? In Java, can we declare a top-level class as protected or private? Sep 12, 2023 pm 07:21 PM

No, we cannot declare top-level classes as private or protected. It can be public or default (no modifiers). If there are no modifiers, there should be default access. Syntax //Atoplevelclass publicclassTopLevelClassTest{ //Classbody} If a top-level class is declared as private, the compiler will report an error, prompting "The modifier private is not allowed here." This means that top-level classes cannot be private, the same applies to protected access

C# .NET Interview Questions & Answers: Level Up Your Expertise C# .NET Interview Questions & Answers: Level Up Your Expertise Apr 07, 2025 am 12:01 AM

C#.NET interview questions and answers include basic knowledge, core concepts, and advanced usage. 1) Basic knowledge: C# is an object-oriented language developed by Microsoft and is mainly used in the .NET framework. 2) Core concepts: Delegation and events allow dynamic binding methods, and LINQ provides powerful query functions. 3) Advanced usage: Asynchronous programming improves responsiveness, and expression trees are used for dynamic code construction.

See all articles