Any member will always have the most restrictive one available - so in this case the accessibility of objectA
is private
. (Assuming it's an instance variable. It makes no sense as a local variable, as they don't have any access rules as such.)
So this:
class Foo
{
Object objectA = new Object();
}
is equivalent to this:
internal class Foo
{
private Object objectA = new Object();
}
The "default to most private" means that for types, the accessibility depends on the context. This:
class Outer
{
class Nested
{
}
}
is equivalent to this:
internal class Outer
{
private class Nested
{
}
}
... because you can't have a private non-nested class.
There's only one place where adding an explicit access modifier can make something more private than it is without, and that's in property declarations:
public string Name { get; set; } // Both public
public string Name { get; private set; } // public get, private set
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…