What Are the Default Access Modifiers in C#

What are the default access modifiers in C#?

The default access for everything in C# is "the most restricted access you could declare for that member".

So for example:

namespace MyCompany
{
class Outer
{
void Foo() {}
class Inner {}
}
}

is equivalent to

namespace MyCompany
{
internal class Outer
{
private void Foo() {}
private class Inner {}
}
}

The one sort of exception to this is making one part of a property (usually the setter) more restricted than the declared accessibility of the property itself:

public string Name
{
get { ... }
private set { ... } // This isn't the default, have to do it explicitly
}

This is what the C# 3.0 specification has to say (section 3.5.1):

Depending on the context in which a
member declaration takes place, only
certain types of declared
accessibility are permitted.
Furthermore, when a member declaration
does not include any access modifiers,
the context in which the declaration
takes place determines the default
declared accessibility.

  • Namespaces implicitly have public declared accessibility. No access
    modifiers are allowed on namespace
    declarations.
  • Types declared in compilation units or namespaces can have public or
    internal declared accessibility and
    default to internal declared
    accessibility.
  • Class members can have any of the five kinds of declared accessibility
    and default to private declared
    accessibility. (Note that a type
    declared as a member of a class can
    have any of the five kinds of declared
    accessibility, whereas a type declared
    as a member of a namespace can have
    only public or internal declared
    accessibility.)
  • Struct members can have public, internal, or private declared
    accessibility and default to private
    declared accessibility because structs
    are implicitly sealed. Struct members
    introduced in a struct (that is, not
    inherited by that struct) cannot have
    protected or protected internal
    declared accessibility. (Note that a
    type declared as a member of a struct
    can have public, internal, or private
    declared accessibility, whereas a type
    declared as a member of a namespace
    can have only public or internal
    declared accessibility.)
  • Interface members implicitly have public declared accessibility. No
    access modifiers are allowed on
    interface member declarations.
  • Enumeration members implicitly have public declared accessibility. No
    access modifiers are allowed on
    enumeration member declarations.

(Note that nested types would come under the "class members" or "struct members" parts - and therefore default to private visibility.)

Default access modifier in C#

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

Default Access modifier of a class in C#

IL does not have the concept of internal, internal classes are represented as private if they are in the root namespace or assembly if they are nested inside another type.

namespace ConsoleApplication1
{
internal class ExplicitInternal
{
private class ExplicitPrivate
{
}

internal class ExplicitNestedInternal
{
}

public class ExplicitNestedPublic
{
}
}

public class ExplicitPublic
{
}

class ImplicitInternal
{
private class ImplicitPrivate
{
}
}
}

becomes

.namespace ConsoleApplication1
{
.class private auto ansi beforefieldinit ConsoleApplication1.ExplicitInternal
extends [mscorlib]System.Object
{
.class nested private auto ansi beforefieldinit ExplicitPrivate
extends [mscorlib]System.Object
{
}

.class nested assembly auto ansi beforefieldinit ExplicitNestedInternal
extends [mscorlib]System.Object
{
}

.class nested public auto ansi beforefieldinit ExplicitNestedPublic
extends [mscorlib]System.Object
{
}
}

.class public auto ansi beforefieldinit ConsoleApplication1.ExplicitPublic
extends [mscorlib]System.Object
{
}

.class private auto ansi beforefieldinit ConsoleApplication1.ImplicitInternal
extends [mscorlib]System.Object
{
.class nested private auto ansi beforefieldinit ImplicitPrivate
extends [mscorlib]System.Object
{
}
}
}

C# Default access modifier of Main() method

If your code appears like this:

static void Main()

then that's a private method. (The static part is orthogonal to accessibility, but is necessary to be an entry point.) In general, the default accessibility of any member is the most private accessibility that you could declare it. So for methods in a class or a struct, that's private. For top-level (non-nested) types it's internal. For any member declared in a class/struct, it's private1. For interface and enum members, it's public.

It's hard to understand exactly what you're seeing via Class View without seeing either your code or a screenshot of Class View, but the default accessibility for a method is definitely private. That's true regardless of whether it's the Main method or not.


1 Explicit interface implementation is a bit odd here, as it's neither private nor public; it's simply not accessible through the type, only through the interface.

What is the default access modifier of a class?

by Default Internal is the access modifier of class

Default visibility for C# classes and members (fields, methods, etc.)?

All of the information you are looking for can be found here and here (thanks Reed Copsey):

From the first link:

Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.

...

The access level for class members and struct members, including nested classes and structs, is private by default.

...

interfaces default to internal access.

...

Delegates behave like classes and structs. By default, they have internal access when declared directly within a namespace, and private access when nested.


From the second link:

Top-level types, which are not nested in other types, can only have internal or public accessibility. The default accessibility for these types is internal.

And for nested types:


Members of Default member accessibility
---------- ----------------------------
enum public
class private
interface public
struct private

method without access modifier

The default accessibility for a type is internal, but the default accesibility of that type's members depends on the type.

Generally speaking, members of a class are private by default, where as members of a struct are public by default. This varies by language; default struct access modifiers for C++ are public, where as for C#, they are private.

Default access modifier for class and variable in C#?

It's funny what you can find by using google. For example the MSDN page about classes. It states:

Classes are internal by default.

And later:

Types declared inside a class without an access modifier default to private



Related Topics



Leave a reply



Submit