1

Possible Duplicate:
Why Visual Studio doesn’t create a public class by default?

msdn link tell us that

http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx

Default member accessibility of Class is private

Please explain whether it is Internal or Private by default ?

Community
  • 1
  • 1
Neo
  • 15,491
  • 59
  • 215
  • 405

5 Answers5

8

The default for all members is "the most private you could specify"1.

So nested types are private by default, but top-level types are internal by default:

namespace Foo
{
    class ThisIsInternal
    {
        class ThisIsPrivate
        {
        }
    }
}

The rules are the same for all types - it doesn't matter whether it's a class, interface, struct, enum or delegate.


1 with the slight exception of specific property getters/setters, where you can only be explicit about one part of the property when you're making it more private.

// The getter is public, the setter is private
public string Foo { get; private set; }
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • And if one wants to check this with reflection, note that there are also different sets of properties to use for nested and non-nested types. For the non-nested type, one should check `typeof(ThisIsInternal).Public` and `typeof(ThisIsInternal).NotPublic`. For the nested type, use `typeof(ThisIsPrivate).IsNestedPublic`, `typeof(ThisIsPrivate).IsNestedFamORAssem`, `typeof(ThisIsPrivate).IsNestedFamily`, `typeof(ThisIsPrivate).IsNestedAssembly`, and `typeof(ThisIsPrivate).IsNestedPrivate`. Using the properties for non-nested types on a nested type, or vice versa, will not give you the answer. – Jeppe Stig Nielsen Feb 02 '13 at 12:23
2

Default member accessibility of Class is private

You are confusing the member (nested class) accessibility with non-nested class accessibility. I have extracted the following from the link you given in the question.

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, reference.

The nested class accessibility is private by default though.

Adil
  • 146,340
  • 25
  • 209
  • 204
2

In the article you mentioned, it is clearly given that if the Class is inside another class, the access will be private by default. Access level can be changed.

It is not possible to create a top level class as private

Ravindra Gullapalli
  • 9,049
  • 3
  • 48
  • 70
1

Here is our test subject:

class AccessibilityTest
{
    class Nested {}
    struct NestedStruct {}
    enum NestedEnum { A }
    int Field;
    void Method() {}
}

Compiling the class with Visual Studio 2012 and then decompiling in one's favorite reflection tool. The result is:

internal class AccessibilityTest
{
    private class Nested
    {
    }
    [StructLayout(LayoutKind.Sequential, Size = 1)]
    private struct NestedStruct
    {
    }
    private enum NestedEnum
    {
        A
    }
    private int Field;
    private void Method()
    {
    }
}

As can be seen, top-level types are internal by default. Nested members are private by default.

Stefan Dragnev
  • 14,143
  • 6
  • 48
  • 52
1

It may seem odd that a class is sometimes internal by default (when top level) and sometimes private (when nested). However, this simply means that all access modifiers are maximally restrictive by default. This also means that almost all usages of the word private are redundant - only those on property accessors are meaningful (they can restrict access to an otherwise non-private property).

So I prefer to think of the default as the consistent one - namely being conceptually private, and the keywords being inconsistent - internal sometimes means keep local to the context, and sometimes means grant access to the entire assembly :-). And private almost always means nothing - it's pure boilerplate which is therefore best left out (except in property accessors).

Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166