In C#, what is the difference between public, private, protected, and having no access modifier?

前端 未结 16 2781
盖世英雄少女心
盖世英雄少女心 2020-11-22 01:12

All my college years I have been using public, and would like to know the difference between public, private, and protected

相关标签:
16条回答
  • 2020-11-22 02:03

    I think it is related to good OOP design. If you are a developer of a library you want to hide the inner workings of your library. That way, you can modify your library inner workings later on. So you put your members and helper methods as private, and only interface methods are public. Methods that should be overwritten should be protected.

    0 讨论(0)
  • 2020-11-22 02:05

    A status of Private indicates that variables can only be accessed by objects of the same class. Protected status extends that access to include descendants of the class as well.

    "from the above table we can see the deference between private and protected... am think both are same ....so what the need for that two separate command"

    Check MSDN link for more information

    0 讨论(0)
  • 2020-11-22 02:06

    Those access modifiers specify where your members are visible. You should probably read this up. Take the link given by IainMH as a starting point.

    Static members are one per class and not one per instance.

    0 讨论(0)
  • 2020-11-22 02:07

    Access modifiers

    From docs.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.

    When no access modifier is set, a default access modifier is used. So there is always some form of access modifier even if it's not set.

    static modifier

    The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created.

    A static class is basically the same as a non-static class, but there is one difference: a static class cannot be externally instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

    However, there is a such thing as a static constructor. Any class can have one of these, including static classes. They cannot be called directly & cannot have parameters (other than any type parameters on the class itself). A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. Looks like this:

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

    Static classes are often used as services, you can use them like so:

    MyStaticClass.ServiceMethod(...);
    
    0 讨论(0)
  • 2020-11-22 02:12

    Careful watch your accessibility of your classes. Public and protected classes and methods are by default accessible for everyone.

    Also Microsoft isn't very explict in showing access modifiers (public, protected, etc.. keywords) when new classes in Visual Studio are created. So, take good care and think about your accessibility of your class because it's the door to your implementation internals.

    0 讨论(0)
  • 2020-11-22 02:14

    enter image description here

    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();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题