views:

184

answers:

7

if I will create new object like this:

Object objectA = new Object();

Which access modifier it will have by default?

+2  A: 

As a class member: private.

If it's a local variable declared within the body of a method, it has no accessibility outside that method. But I'm guessing you already knew that.

Dan Tao
+3  A: 
void Foo()
{
    // private in method scope
    Object objectA = new Object();
}

class Bar()
{
    // private in class scrope
    Object objectA = new Object();
}
abatishchev
+15  A: 

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
Jon Skeet
Seems like you ought to include an actual member field in one of those classes and show that it translates to `private` as well, no? It seems to me that's the main question on the OP's mind: the accessibility of members fields.
Dan Tao
@Dan: Isn't that explained in the first sentence? The example was meant to demonstrate how for *types* the default access can vary based on context. It's extra information, after answering the actual question in the first sentence.
Jon Skeet
@Jon: Yeah, I just thought it'd be particularly illustrative to include it in the actual code sample; that's all. I think it's good that you show the equivalence between code with and without the modifiers.
Dan Tao
As a rule I've always said that access modifiers should be explicitly declared (I think StyleCop agrees with me on this one). Incidentally, on the premise that 'private' means not accessible outside it's own scope, are 'private' and 'internal' not technically equivalent on an outer class, since its scope is the assembly itself?
Flynn1179
@Dan: Personally I thought it was clear enough already, but I've added an extra example.
Jon Skeet
@Jon: Thanks for humoring me ;)
Dan Tao
@Flynn1179: No, `private` and `internal` aren't equivalent at all on an outer class: `private` is invalid, as there's no "containing" type to have access to it. (Basically your premise isn't quite accurate.) I agree on being explicit though (although my views on this have changed over time; I used to support "use the default where you can").
Jon Skeet
I didn't know about the private set, thanks for the information!
Richard J. Ross III
+3  A: 

by default it is private

B-Rain
A: 

The class/type itself will default to "internal". The object you create will default to "private".

Liggi
A: 

Classes and structs are declared as internal by default!

Read more here

rudigrobler
Nested types aren't... see my answer.
Jon Skeet
The OP is asking about objects, not types.
Dan Tao
@Jon Skeet, tnx for the great answere!!!
rudigrobler
+1  A: 

For class members and struct members, including nested classes and structs, private is the default.

For classes and structs - internal is the default

You can check out MSDN for further reading..

Oren A
ma nishma haver! eifo ata gar? ani garti be kiryat gat.
Ragims
I think you should mark Jon Skeet's answer as the accepted one.
Oren A
raziti laasot masheu naim leha. ani ihaol livhor lemi latet et nekudot. haval. lo garti be israel 6 shanim, ve ze haia naim meod lepagesh po mishehu mi aaretz.
Ragims
So you can Email me. [email protected]. But you can't prefer my answer over a better answer. And you can't use non-English languages here. Respect house rules (-:
Oren A