Friday, March 4, 2011

Default Class Accessibility in C#

by default is a class:

  1. private ?
  2. internal ?
  3. sealed ?
From stackoverflow
  • internal

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

    Jon Skeet : Your answer omits the case of a nested type, where the default is private.
  • Top-level types, which are not nested into other types, can only have internal or public accessibility. The default accessibility for these types is internal.

    Accessibility Levels (C#) on MSDN

  • Also, it is not sealed by default. I believe nested classes are private by default.

  • The default for non-nested types is internal. The default for nested types is private. In both cases the default (for classes) is unsealed.

    The general rule for all members is that if you don't specify an access modifier, it's as private as it can be. The single exception for this is properties which can make one part (i.e. the getter or the setter) more private than the overall property by specifying an access modifier, e.g.

    public string Foo { get; private set; }
    

0 comments:

Post a Comment