13

Possible Duplicate:
Why are my privates accessible?
Why are private fields private to the type, not the instance?

Most probably I am missing an obvious fact but I cannot really see the reason:

When I override the Equals() method and when I cast the object to my type, I am able to call its private members without any problem!!!

I am initializing an instance and I expect its private members not to be reachable.

But why the casted object opens its privates to me in the Equals() method?

See the Equals implementation on the sample code below and see how I am reaching the private fields on the "that" instance:

 public class Animal
 {
     private string _name;
     private int _age;

     public Animal(int age, string name)
     {
         _name = name;
         _age = age;
     }

     public override bool Equals(object obj)
     {
         var that = (Animal) obj;


         //_name and _age are available on "that" instance
         // (But WHY ??? )
         return
             this._age == that._age
             && this._name == that._name; 

     }
 }


    class Program
    {
        static void Main(string[] args)
        {
            var cat1 = new Animal(5, "HelloKitty");
            var cat2 = new Animal(5, "HelloKitty");

            Console.Write(cat1.Equals(cat2));
            Console.Read();
        }
    }
Community
  • 1
  • 1
pencilCake
  • 51,323
  • 85
  • 226
  • 363

3 Answers3

22

Private members are private to a class, not to an instance.

Inside the Animal class, you can access any private members of any instance of Animal that you are passed (or, in this case, successfully cast to).

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 5
    To clarify for the OP, this behavior has nothing at all to do with the `Equals()` method; it is not special in this regard. Any methods defined on `Animal` (including static methods) can access private members on any `Animal` object it is given. – cdhowie Sep 06 '11 at 14:31
8

The private members are available because they are private to the class, not the instance. Because you are within the Animal class you can access private members, even of a different instance.

Here's a link to a prior discussion of this language feature: Why are private fields private to the type, not the instance?

Community
  • 1
  • 1
FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
1

Because public override bool Equals(object obj) is part of the class Animal.

Maybe your're getting confused because you were thinking the Equals was a static member of Animal and, this way, not being able to see the private members of Animal. Or maybe not xD.

Canella
  • 444
  • 4
  • 15
  • 1
    This behavior has nothing to do with the staticness of the method. – cdhowie Sep 06 '11 at 14:33
  • Indeed it doesn't. It's about casting an outsider object to the Animal class and being able to see it's private members, even being an outsider object (but inside the Animal class, and thus being able to see it's private members, even if object is not an 'Animal'). I was just trying to guess why mr. @pencilCake was confused. – Canella Sep 06 '11 at 14:57