C#访问修饰符

C#访问修饰符 首页 / C#入门教程 / C#访问修饰符

C#访问修饰符或说明符是用于指定C#应用程序中变量和函数的可访问性或范围的关键字。

C#提供了五种类型的访问说明符。

  1. Public
  2. Protected
  3. Internal
  4. Protected internal
  5. Private

无涯教程可以选择其中任何一个来保护数据。下表描述了每个组件的可访问性。

Access SpecifierDescription
Public它指定不受限制的访问。
Protected它指定访问权限仅限于包含类或派生类。
Internal它指定访问权限限于当前程序集。
protected internal它指定访问权限仅限于当前汇编或从包含类派生的类型。
Private它指定访问范围仅限于包含类型。

现在,创建示例来检查每个访问说明符的可访问性。

C# Public 修饰符

它使数据可以公开访问。

using System;
namespace AccessSpecifiers
{
    class PublicTest
    {
        public string name = "Shantosh Kumar";
        public void Msg(string msg)
        {
            Console.WriteLine("Hello " + msg);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            PublicTest publicTest = new PublicTest();
            //Accessing public variable
            Console.WriteLine("Hello " + publicTest.name);
            //Accessing public function
            publicTest.Msg("Peter Decosta");
        }
    }
}

输出:

Hello Shantosh Kumar
Hello Peter Decosta

C# Protected 修饰符

它可以在类内访问,并且范围有限。在继承的情况下,也可以在子类或子类中访问它。

using System;
namespace AccessSpecifiers
{
    class ProtectedTest
    {
        protected string name = "Shashikant";
        protected void Msg(string msg)
        {
            Console.WriteLine("Hello " + msg);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ProtectedTest protectedTest = new ProtectedTest();
            //Accessing protected variable
            Console.WriteLine("Hello "+ protectedTest.name);
            //Accessing protected function
            protectedTest.Msg("Swami Ayyer");
        }
    }
}

输出:

Compile time error
'ProtectedTest.name' is inaccessible due to its protection level.

示例2

在这里,无涯教程通过继承访问子类中的受保护成员。

using System;
namespace AccessSpecifiers
{
    class ProtectedTest
    {
        protected string name = "Shashikant";
        protected void Msg(string msg)
        {
            Console.WriteLine("Hello " + msg);
        }
    }
    class Program : ProtectedTest
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            //Accessing protected variable
            Console.WriteLine("Hello " + program.name);
            //Accessing protected function
            program.Msg("Swami Ayyer");
        }
    }    
}

输出:

Hello Shashikant
Hello Swami Ayyer

C# Internal修饰符

INTERNAL关键字用于指定变量和函数的内部访问说明符。此说明符只能在同一程序集中的文件中访问。

using System;
namespace AccessSpecifiers
{
    class InternalTest
    {
        internal string name = "Shantosh Kumar";
        internal void Msg(string msg)
        {
            Console.WriteLine("Hello " + msg);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            InternalTest internalTest = new InternalTest();
            //Accessing internal variable
            Console.WriteLine("Hello " + internalTest.name);
            //Accessing internal function
            internalTest.Msg("Peter Decosta");
        }
    }
}

输出:

Hello Shantosh Kumar
Hello Peter Decosta

C# Protected Internal修饰符

声明了受保护的内部的变量或函数可以在声明它的程序集中访问。也可以在另一个程序集中的派生类中访问它。

using System;
namespace AccessSpecifiers
{
    class InternalTest
    {
        protected internal string name = "Shantosh Kumar";
        protected internal void Msg(string msg)
        {
            Console.WriteLine("Hello " + msg);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            InternalTest internalTest = new InternalTest();
            //Accessing protected internal variable
            Console.WriteLine("Hello " + internalTest.name);
            //Accessing protected internal function
            internalTest.Msg("Peter Decosta");
        }
    }
}

输出:

Hello Shantosh Kumar
Hello Peter Decosta

C# Private 修饰符

私有访问说明符用于指定变量或函数的私有可访问性。它是限制性最强的,并且只能在声明它的类体中访问。

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-access-modifiers.html

来源:LearnFk无涯教程网

using System;
namespace AccessSpecifiers
{
    class PrivateTest
    {
        private string name = "Shantosh Kumar";
        private void Msg(string msg)
        {
            Console.WriteLine("Hello " + msg);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            PrivateTest privateTest = new PrivateTest();
            //Accessing private variable
            Console.WriteLine("Hello " + privateTest.name);
            //Accessing private function
            privateTest.Msg("Peter Decosta");
        }
    }
}

输出:

Compile time error
'PrivateTest.name' is inaccessible due to its protection level.

Private修饰符示例

using System;
namespace AccessSpecifiers
{
    class Program
    {
        private string name = "Shantosh Kumar";
        private void Msg(string msg)
        {
            Console.WriteLine("Hello " + msg);
        }
        static void Main(string[] args)
        {
            Program program = new Program();
            //Accessing private variable
            Console.WriteLine("Hello " + program.name);
            //Accessing private function
            program.Msg("Peter Decosta");
        }
    }
}

输出:

Hello Shantosh Kumar
Hello Peter Decosta

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

教程推荐

大模型应用开发实战 -〔黄佳〕

深入浅出分布式技术原理 -〔陈现麟〕

网络排查案例课 -〔杨胜辉〕

大厂晋升指南 -〔李运华〕

爱上跑步 -〔钱亮〕

分布式协议与算法实战 -〔韩健〕

说透中台 -〔王健〕

微服务架构核心20讲 -〔杨波〕

赵成的运维体系管理课 -〔赵成〕

好记忆不如烂笔头。留下您的足迹吧 :)