大学期间我一直在使用public,想知道public, private和protected之间的区别吗?

还有,相对于什么都没有,静态有什么作用呢?


当前回答

访问修饰符

从learn.microsoft.com:

public The type or member can be accessed by any other code in the same assembly or another assembly that references it. private The type or member can only be accessed by code in the same class or struct. protected The type or member can only be accessed by code in the same class or struct, or in a derived class. private protected (added in C# 7.2) The type or member can only be accessed by code in the same class or struct, or in a derived class from the same assembly, but not from another assembly. internal The type or member can be accessed by any code in the same assembly, but not from another assembly. protected internal The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

如果没有设置访问修饰符,则使用默认访问修饰符。所以总有某种形式的访问修饰符,即使它没有设置。

静态修饰符

类上的静态修饰符意味着该类不能被实例化,并且它的所有成员都是静态的。静态成员只有一个版本,不管创建了多少个其封闭类型的实例。

静态类基本上与非静态类相同,但有一个区别:静态类不能被外部实例化。换句话说,不能使用new关键字创建类类型的变量。因为没有实例变量,所以可以使用类名本身访问静态类的成员。

然而,存在一种静态构造函数。任何类都可以有其中一个,包括静态类。它们不能被直接调用,也不能有参数(除了类本身的任何类型参数)。在创建第一个实例或引用任何静态成员之前,会自动调用静态构造函数来初始化类。看起来是这样的:

static class Foo()
{
    static Foo()
    {
        Bar = "fubar";
    }
    
    public static string Bar { get; set; }
}

静态类通常被用作服务,你可以这样使用它们:

MyStaticClass.ServiceMethod(...);

其他回答

当前访问修饰符的另一种可视化方法(c# 7.2)。希望这个模式能帮助你更容易地记住它 (请按此图片浏览。)

外内

如果你很难记住两个词的访问修饰符,记住由外而内。

Private protected:外部私有(相同程序集)内部受保护(相同程序集) 保内:保外(同一组件)保内(同一组件)

这些访问修饰符指定成员可见的位置。你应该读读这个。以IainMH给出的链接为起点。

静态成员是每个类一个,而不是每个实例一个。

using System;

namespace ClassLibrary1
{
    public class SameAssemblyBaseClass
    {
        public string publicVariable = "public";
        protected string protectedVariable = "protected";
        protected internal string protected_InternalVariable = "protected internal";
        internal string internalVariable = "internal";
        private string privateVariable = "private";
        public void test()
        {
            // OK
            Console.WriteLine(privateVariable);

            // OK
            Console.WriteLine(publicVariable);

            // OK
            Console.WriteLine(protectedVariable);

            // OK
            Console.WriteLine(internalVariable);

            // OK
            Console.WriteLine(protected_InternalVariable);
        }
    }

    public class SameAssemblyDerivedClass : SameAssemblyBaseClass
    {
        public void test()
        {
            SameAssemblyDerivedClass p = new SameAssemblyDerivedClass();

            // NOT OK
            // Console.WriteLine(privateVariable);

            // OK
            Console.WriteLine(p.publicVariable);

            // OK
            Console.WriteLine(p.protectedVariable);

            // OK
            Console.WriteLine(p.internalVariable);

            // OK
            Console.WriteLine(p.protected_InternalVariable);
        }
    }

    public class SameAssemblyDifferentClass
    {
        public SameAssemblyDifferentClass()
        {
            SameAssemblyBaseClass p = new SameAssemblyBaseClass();

            // OK
            Console.WriteLine(p.publicVariable);

            // OK
            Console.WriteLine(p.internalVariable);

            // NOT OK
            // Console.WriteLine(privateVariable);

            // Error : 'ClassLibrary1.SameAssemblyBaseClass.protectedVariable' is inaccessible due to its protection level
            //Console.WriteLine(p.protectedVariable);

            // OK
            Console.WriteLine(p.protected_InternalVariable);
        }
    }
}

 using System;
        using ClassLibrary1;
        namespace ConsoleApplication4

{
    class DifferentAssemblyClass
    {
        public DifferentAssemblyClass()
        {
            SameAssemblyBaseClass p = new SameAssemblyBaseClass();

            // NOT OK
            // Console.WriteLine(p.privateVariable);

            // NOT OK
            // Console.WriteLine(p.internalVariable);

            // OK
            Console.WriteLine(p.publicVariable);

            // Error : 'ClassLibrary1.SameAssemblyBaseClass.protectedVariable' is inaccessible due to its protection level
            // Console.WriteLine(p.protectedVariable);

            // Error : 'ClassLibrary1.SameAssemblyBaseClass.protected_InternalVariable' is inaccessible due to its protection level
            // Console.WriteLine(p.protected_InternalVariable);
        }
    }

    class DifferentAssemblyDerivedClass : SameAssemblyBaseClass
    {
        static void Main(string[] args)
        {
            DifferentAssemblyDerivedClass p = new DifferentAssemblyDerivedClass();

            // NOT OK
            // Console.WriteLine(p.privateVariable);

            // NOT OK
            //Console.WriteLine(p.internalVariable);

            // OK
            Console.WriteLine(p.publicVariable);

            // OK
            Console.WriteLine(p.protectedVariable);

            // OK
            Console.WriteLine(p.protected_InternalVariable);

            SameAssemblyDerivedClass dd = new SameAssemblyDerivedClass();
            dd.test();
        }
    }
}

访问修饰符

从learn.microsoft.com:

public The type or member can be accessed by any other code in the same assembly or another assembly that references it. private The type or member can only be accessed by code in the same class or struct. protected The type or member can only be accessed by code in the same class or struct, or in a derived class. private protected (added in C# 7.2) The type or member can only be accessed by code in the same class or struct, or in a derived class from the same assembly, but not from another assembly. internal The type or member can be accessed by any code in the same assembly, but not from another assembly. protected internal The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

如果没有设置访问修饰符,则使用默认访问修饰符。所以总有某种形式的访问修饰符,即使它没有设置。

静态修饰符

类上的静态修饰符意味着该类不能被实例化,并且它的所有成员都是静态的。静态成员只有一个版本,不管创建了多少个其封闭类型的实例。

静态类基本上与非静态类相同,但有一个区别:静态类不能被外部实例化。换句话说,不能使用new关键字创建类类型的变量。因为没有实例变量,所以可以使用类名本身访问静态类的成员。

然而,存在一种静态构造函数。任何类都可以有其中一个,包括静态类。它们不能被直接调用,也不能有参数(除了类本身的任何类型参数)。在创建第一个实例或引用任何静态成员之前,会自动调用静态构造函数来初始化类。看起来是这样的:

static class Foo()
{
    static Foo()
    {
        Bar = "fubar";
    }
    
    public static string Bar { get; set; }
}

静态类通常被用作服务,你可以这样使用它们:

MyStaticClass.ServiceMethod(...);

public means that it can be accessed by any class in any assembly, which includes the class itself. protected internal means it can be accessed by the class itself (in the class definition), and it can be accessed by any class in the current assembly, but outside of the assembly it can only be accessed by a class that inherits the class, or by the class itself (if it is a partial class) – basically it means internal inside the assembly and protected outside of the assembly. protected means it can only be accessed by the class itself, or accessed by a class that inherits it and that class can be in any assembly internal means it can be accessed by the class itself or by any class in the assembly but can't be accessed at all outside of the assembly unless by the class itself (i.e. it is a partial class) private protected means it can only be accessed by the class itself, or it can be accessed by a class that inherits it and only if that class is in the current assembly. Outside of the assembly, it can only be accessed by the class itself (i.e. it is a partial class) – basically combines internal and protected, or another way of putting it is it's private outside of the assembly and protected inside the assembly. private means that it can only be accessed by the class itself no access modifier: The default access for everything in C# is "the most restricted access you could declare for that member"., which is private for a member / method / nested class in a class and internal for a non-nested class.

In the above text 'accessed' means accessed via an object of the class type, which within a method of the class itself will be the implicit this object, or perhaps the method instantiates an explicit object of the current class type and accesses it through that object. Both are considered to be being accessed by the class itself and therefore the access rules are the same. This also applies to an access being performed from a static method, or when it is a static member / method being accessed, except the access is performed using the class scope and not and object. A member / method of a static class needs to be explicitly made static or it won't compile.

没有嵌套的类可以是公共的,也可以是内部的,默认情况下是内部的。嵌套的类可以是任何访问类型,如果父类是静态的,它就不需要是静态的,它的成员也不需要是静态的。内部类意味着只能实例化它或从当前程序集访问它的静态成员。

您可以在内部或私有嵌套类中拥有公共成员/方法/嵌套类——只有访问说明符(在正在进行的访问的完全限定名称中)低于当前正在进行的访问所需的级别才能阻止访问。

Inheritance in C# is always public unlike C++, which can privately or protectedly inherit, which then changes the access of all classes that then inherit from the class that is inheriting from this class, as well as the access via object / via class scope of type of the class that inheriting privately / protectedly from the class and the class that is inheriting from the class that is inheriting privately / protectedly form the class, and so on. The access is changed such that all access modifiers less restrictive than private or protected are made private and protected respectively.