1

I'm reading the head first c# book and don't quite understand what this means. "Any class can see private fields in another instances of the same class"

4

Consider these classes:

class A
{
    private int foo;

    public A(int f) { this.foo = f; }

    // works
    public int AddTo(A other) { return this.foo + other.foo; }

    // doesn't work: 'B.bar' is inaccessible due to its protection level
    public int AddTo(B other) { return this.foo + other.bar; }
}    

class B
{
    public B(int b) { this.bar = b; }

    private int bar;
}

var a1 = new A(123);
var a2 = new A(456);

var result = a1.AddTo(a2);

Here, the a1 instance of A accesses a2's private foo value. result will be 579.

However, the second method, AddTo(B) doesn't work -- it won't even compile -- because A instances don't have access to B instances private members.

var b = new B(789);
var result2 = a1.Add2(b);

Here, a1 tries to access b's private bar member, but because a1 is an instance of A and b is an instance of B, the latter's private bar member is inaccessible.

  • I would separate that code block into two examples, to show the one that works vs. the one that doesn't. I was a little confused at first reading this. – Snoop Jun 18 '16 at 18:54
  • Edited. Hopefully this is more clear. – FetchLaVache Jun 18 '16 at 19:45
  • BTW, this has an interesting and very important consequence: the defining difference between objects and Abstract Data Types is that objects cannot see each other's privates, whereas ADT instances can see privates of other instances of the same type. Since instances of the same class can see each other's privates, they aren't objects, they are ADT instances. OTOH, instances of the same interface can not see each other's privates and are thus objects. So, in C♯, classes define ADTs, not objects, interfaces define objects. IOW: it's not classes that make C♯ object-oriented, it's interfaces. – Jörg W Mittag Jun 19 '16 at 0:28
  • And a program that uses classes as types (i.e. use a class (or struct) as the type of a local variable, field, property, method return type, method parameter, generic type argument, cast operator, as operator, instanceof operator, is operator, delegate type, etc.) is not object-oriented. Only interfaces can be used as types, classes/structs can only be used as factories, i.e. a class/struct name can only appear directly after the new operator. Note: there's nothing wrong with writing and/or using ADTs in C♯, but if you want to write OO, you have to follow these rules. – Jörg W Mittag Jun 19 '16 at 0:34
0

If class Foo has a private member, such as

public class Foo
{
    private int _bar;
}

Then _bar is accessible from within any instance of Foo.

public class Foo
{
    private int _bar;

    public bool MatchesOtherFoo(Foo other)
    {
        return _bar == other._bar;
    }
}

You couldn't access _bar from another class, but you can access it from other instances of the same class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.