TechCulture

Icon

A blog on Technology | Culture | Life | People | Experiences

Access Modifiers in .NET

Here is some interesting information about Access Modifiers in .NET that you may not know.

  1. .NET supports 4 access modifiers – public, protected, private and internal. First three are obvious, the last one – internal, provides public access within that assembly, and private access outside it for type members. When used with types, internal hides accessibility of type outside the assembly.

    Typically, these access modifiers are used only one at a time. The only exception is ‘protected internal‘ – it is used to specify public access within the assembly, and protected access outside it.

  2. All the members of an enumeration have public access by default. You cannot provide any other access. Ditto for interfaces.
  3. Members of a struct can only have public, internal and private acess. Having protected acess members doesn’t make any sense structs cannot be inherited from. The default accessibility of members in structs and classes is private. (This is interesting in comparison to C++. In C++ there is no difference between classes and structs except the default access is public in structs and private in classes)
  4. While declaring interfaces, classes, structs and enumeration, the allowed access modifers are public and internal. These control access at assembly level, not namespace level. The default is internal.
  5. The exception to the above rule is when you declare classes, structs, enumerations or interfaces within another class or struct, instead of at the namespace level. While doing so, you may use public, internal and private within structs and public, protected, private, internal and protected internal within classes.
  6. .NET supports only public inheritance. So an access modifier is never used in inheritance relationships (unlike C++) – public is implied by default.
  7. Classes and structs may have static constructors. Static constructors are parameterless, and are used to initialize static members when that type is first referenced. With static constructors, no access modifiers are permitted.
  8. Here are some restrictions on using accessibility levels (copied from MSDN/C#)
    1. The direct base class of a class type must be at least as accessible as the class type itself
    2. The explicit base interfaces of an interface type must be at least as accessible as the interface type itself.
    3. The return type and parameter types of a delegate type must be at least as accessible as the delegate type itself.
    4. The type of a constant must be at least as accessible as the constant itself.
    5. The type of a field must be at least as accessible as the field itself.
    6. The return type and parameter types of a method must be at least as accessible as the method itself.
    7. The type of a property must be at least as accessible as the property itself.
    8. The type of an event must be at least as accessible as the event itself.
    9. The type and parameter types of an indexer must be at least as accessible as the indexer itself.
    10. The return type and parameter types of an operator must be at least as accessible as the operator itself.
    11. The parameter types of a constructor must be at least as accessible as the constructor itself.

Filed under: Programming

2 Responses

  1. Sathishkumar says:

    I have doubt about your explanation on protected internal.
    Please go through your statements:
    Typically, these access modifiers are used only one at a time. The only exception is ‘protected internal‘ – it is used to specify public access within the assembly, and protected access outside it.

    I think, protected internal means, protected for within the assembly, and private access for outside.
    Please let me know, if any.
    regards,
    S. Sathish Kumar

  2. saurabh says:

    please explain protected and private modifier in c#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: