what is the default access modifier in the class if not specified

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
P

puzzlecracker

/*1 not specified*/class {

/*2 not specified*/ void Foo(){}

}

Will they, like in c++, default to private?

Thanks
 
puzzlecracker said:
/*1 not specified*/class {

/*2 not specified*/ void Foo(){}

}

Will they, like in c++, default to private?

The default accessibility is always the most private possible. So for
top-level (non-nested) types, the default access is internal. For
nested types, it's private. For methods, fields, properties, events,
constructors and constants, it's always private.

There's *one* exception to this rule, which is property access. If you
have a property like this:

public int Foo
{
get { ... };
set { ... };
}

you could make either part of it private like this:

public int Foo
{
get { ... };
private set { ... };
}

That's the only place (that I'm aware of) in C# where explicitly
specifying an access level gives you a more restricted level than the
default. (Here the default access for each part of the property is
whatever access the overall property is specified as.)
 
/*1 not specified*/class {

   /*2 not specified*/ void Foo(){}

}

Will they, like in c++, default to private?

Thanks

Puzzle,

internal for top level classes and private for nested classes.

-Jay
 
/*1 not specified*/class {

/*2 not specified*/ void Foo(){}

}

Will they, like in c++, default to private?

Thanks

internal
member's (properties, attributes & methods) are private by defualt
 
by assembly you mean package/namespace?

The two are different concepts. If, by "package", you mean the dll/
exe, then yes: but the correct term in .NET is assembly.

A namespace is purely a notational device to help you (the developer)
organise code; it has no specific affect on access, and is orthogonal
to the assembly concept: an assembly can contribute multiple
namespaces, and multiple assemblies can contribute to the same
namespace.

Marc
 
Back
Top