2

guys,thanks for your time. As we known,the key words 'Private' and 'Protected' are very useful to keep some methods,fields,properties from invalid accessing outside the class.But I had got a problem in a specific circumstance.Here is the example:

Class Node
{
  public string Name {get; private set;}//Private setting authority
  public int ID {get;set}

  public Node Previous {get;set;}
  public Node Next {get;set;}

  public void Test()
  {
     this.Name='Valid';
     this.Next.Name='Invalid';//Is this valid??
  }
}

Just like the code above,we got a 'Node' Class here with two properties named 'Previous' and 'Next' which are also 'Node' type.Now let's focus on the 'Test()' method.It's valid to Set 'this.Name',but is that weird to set 'this.Next.Name'?Unfortunately,the complier treats this as a valid format!

So far as I was taught,'Private' means it can not be used outside the class.But in this situation,I can access to any non-public members( private/protected etc..) of 'Previous' and 'Next'.

This really confuses me a lot,would u plz give some advices,thanks!

Claw
  • 474
  • 5
  • 16
  • 4
    You're wondering why you can access another _instance_'s setter? Your code is in the same class, i.e.: `Node`. – canon Oct 28 '13 at 02:01
  • See also [Why are private fields private to the type, not the instance?](http://stackoverflow.com/questions/6983553/) – Jeppe Stig Nielsen Jan 28 '15 at 10:32

3 Answers3

2

The Test() method is a member of the Node class, so it has access to private members; which means it can set the Name property. It doesn't matter that it's another instance of the class -- it's still the same class.

A method in a different class does not have access to the private members of the Node class, so it can get the Name property, but not set it.

Example:

class Node
{
    public string Name { get; private set; }
    public int ID { get; set; }
    public Node Previous { get; set; }
    public Node Next { get; set; }

    public void Test()
    {
        this.Name = "Valid";

        // Works
        this.Next.Name = "Invalid";
    }
}

class Other
{
    public void Test()
    {
        Node node = new Node();

        // Doesn't Work
        node.Name = "Invalid";
    }
}
Tim S
  • 2,309
  • 16
  • 23
  • Thanks!So the instance or not doesn't matter.The truth is the private property is modified in the definition scope.Is that right?But is that a good idea or right way to modify some other instance's private property even it's in the same type? – Claw Oct 28 '13 at 02:51
0

It's valid, but you have to make sure Next is defined

public void Test()
{
     this.Next = new Node();
     this.Name="Valid";
     this.Next.Name="Invalid";//Is this valid??

}

It's accessible since you're inside the Node class, so private is in scope. Basically, a Node is a Node

TGH
  • 38,769
  • 12
  • 102
  • 135
  • Thanks!As u said,"a Node is a Node"!But it's so weird to do so even it's valid.Just like an overtone which break down the whole harmony! – Claw Oct 28 '13 at 03:00
0

From the C# manual

private The type or member can be accessed only by code in the same class or struct.

You are confusing this with scope access and not namespace access.. Private does not mean the member is localized by object instance. It means it is localized by defining namespace.

It does not matter if that class is instantiated or not. For example; you can access private static members.

So your code is valid, because you are accessing this.Next.Name from the same namespace. Which this has private access to.

Reactgular
  • 52,335
  • 19
  • 158
  • 208
  • Thanks for answering!Usually,I regard this Access Key Words as some instance things! So it make me feel so weird to modify some other instance's private property even the instance is in the same type.Although it's valid, but is that a good idea to do so like the example above? – Claw Oct 28 '13 at 02:55