-1

I have this class:

class Foo
    {
        private int _value;

        public static int operator +(Foo foo1, Foo foo2)
        {
            return foo1._value + foo2._value;
        }
    }

Why am I able to access foo1._value, which is explicitly declared as private?

Nigel
  • 2,961
  • 1
  • 14
  • 32
  • 1
    Access levels determine which _types_ (classes / structs / interfaces) may access things, not which _objects_ may access things. Even though `foo1` and `foo2` are private, the operator declared in the `Foo` class may access them. – Joe Sewell Oct 22 '21 at 18:59

1 Answers1

0

It is private to Foo. Your method is part of Foo, hence it has full access to itself. If no method of Foo could touch that field: it could never be used at all.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900