开发者

Difference between public and private variables

开发者 https://www.devze.com 2023-01-30 18:51 出处:网络
namespace hi { class hithere { public int numberOne = 12; private int numberTwo = 12; static void yo() { } } } C开发者_Go百科an someone tell me the difference between the variables numberOne and nu
namespace hi
{
    class hithere
    {
         public int numberOne = 12;
         private int numberTwo = 12;

         static void yo()
         {
         }
    }
}

C开发者_Go百科an someone tell me the difference between the variables numberOne and numberTwo in this code excerpt?


From the accessibility levels at MSDN:

public Access is not restricted.

protected Access is limited to the containing class or types derived from the containing class.

internal Access is limited to the current assembly.

protected internal Access is limited to the current assembly or types derived from the containing class.

private Access is limited to the containing type.


Public variables are accessible from out side classes but private one just accessible for current class and inner classes:

public class VarClass
{
    public int publicID = 10;
    private int privateID = 100;
    public VarClass()
    {
        Console.WriteLine(publicID);
        Console.WriteLine(privateID);
    }

    public class InnerClass
    {
        public InnerClass()
        {
            VarClass c = new VarClass();
            Console.WriteLine(c.privateID);
            Console.WriteLine(c.publicID);
        }
    }
}

public class OuterClass
{
    public OuterClass()
    {
        VarClass c = new VarClass();
        Console.WriteLine(c.privateID); // Compile Error
        Console.WriteLine(c.publicID);
    }
}


Other objects can access NumberOne, but they can't access numberTwo.

So I can do Your_Object.numberOne = 14; but I can't do Your_Object.numberTwo= 14;

Now I might be able to access the numberTwo through reflection depending on the permissions set up in the application, but I can't directly.

Variables in C#
Reflection in C#
Accessors in C#


in general, PUBLIC means any other code outside the class can change its value. PRIVATE are only able to be used/changed IN the class itself. Additionally, if you build classes that are derived from another, and you want the child-level class to be able to use/change values of its parent, but not other generic code outside the class, use PROTECTED.


Search for "Encapsulation". There are so many easy materials to study it.


In the simplest terms numberOne being marked as public means that if you create an instance of your hithere class this member variable will be accessible. For example:

hithere h = new hithere()
h.numberOne = 42;

numberTwo being private means that this is not accessible and can only be used within the hithere class. So taking the above example further:

hithere h = new hithere()
h.numberOne = 42;
h.numberTwo = 84;

The last line would cause a compiler error as you cannot access numberTwo as this is private.

It is probably worth spending some time reading up on access modifiers, a quick google will find many examples, e.g:

http://www.devsource.com/c/a/Techniques/Understanding-C-Class-and-Member-Modifiers/ http://msdn.microsoft.com/en-us/library/ba0a1yw2%28v=vs.80%29.aspx

Additionally your hithere class does not have an access modifier defined therefore the compiler assumes this to be private. As such if you were to reference your library from another you would not be able to create instances of the hithere class.


public and private are access modifiers for members. This refers to who can access the members directly through code. public means that access is not limited so that anyone can access the member directly through code. private means that access is limited only to the containing class. So everyone can access numberOne directly through code, but only the containing class can access numberTwo directly through code.

There are a total of five access modifiers:

public: access is not limited

protected: access is limited to the containing class or classes derived from the containing class

internal: access is limited to the containing assembly

protected internal: this is an OR of protected and internal so that access is limited to the containing class or classes derived from the containing class OR the containing assembly

private: access is limited to the containing class

Note that the clause "directly through code" is critical here; it is possible to access these members using reflection.

The relevant section of the C# specification is §3.5, especially §3.5.2.


It means that if you have another class like this:

namespace hi
{
    class hithere2
    {
         static void yo2()
         {
             hithere h = new hithere();
             h.numberOne = 2;
             h.numberTwo = 3;
         }
    }
}

The second one errors, but the first is OK.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号