Thursday, March 3, 2016

Access Modifiers in C#

In today's blog post, I will be discussing the various access modifiers available in C#.

ACCESS MODIFIERS

The access modifiers help decide the accessibility levels of the members or types. They are the first step to learning object oriented programming. With appropriate access modifiers , we decide what is exposed and what is hidden from other parts of the code.

Access Modifiers in C#


There are 4 access modifiers:
i) Public: These is the one with least restriction. These are available for access from anywhere outside.

ii) Private: This makes sure that access is available only within the containing type. These are not available outside.

iii) Protected: Using this keyword, access is limited to containing class or any class inheriting from this.

iv) Internal: Access is limited to the current assembly. Within the assembly it can be accessed anywhere.


Protected Internal:
There is one more access modifier which is a combination of protected and internal. In my opinion, it's a little weird, because when I think of something as protected internal, I used to assume an "AND" condition between protected and internal. I thought that it should be accessible within the assembly and only containing type and derived types within the assembly should be able to access it.
But, the reality is, it's a more forgiving "OR" condition between protected and internal. So it can be accessed from anywhere within the assembly and also any derived class can also access it, even if it's outside the assembly.


Accessibility for Types

Depending on where the type or member is declared, it can be associated with different accessibility levels. If none is specified then there is a default accessibility associated.
i) Top level types: Top level types can only have internal or public accessibility. The default is internal for them.
ii) Enum: Enum members are always by default public. There is no access modifier allowed for enum members.

iii) Class: A class members can have any of the above five access modifiers associated. If no access modifiers is provided, by default, the class members are private.

iv) Interface: An interface members are always by default public. There is no access modifier allowed for interface members.

v) Struct: A struct members are by default private. They can also have public or internal access modifier.

Access modifiers are not allowed on namespaces. They are allowed only on the types mentioned above.

Conclusion

These access modifiers lay the foundation of our object oriented programming and therefore are very important to understand. Different accessibility levels allow us to expose the correct amount of functionality which is really important for security and maintainability of the application.

For future updates to my weekly blog, please subscribe to my blog via the "Subscribe To Weekly Post" feature at the right and follow me on Twitter. Until then Happy Coding :)

5 comments:

  1. The wording is incorrect. Enums, classes, interfaces, structs and all other types can have any modifiers. It's the members of enums and interfaces and structs you are talking about, not the types themselves.

    To be clear, nested types (classes, enums, delegates, interfaces, structs) can have any modifiers. Top-level, non-nested types can only be internal or public and are internal by default.

    Enum members and interface members are always public. Structs (and sealed classes) have no concept of inheritance, so protected makes no sense for them.

    ReplyDelete
    Replies
    1. Hi Joseph,
      Thanks for pointing out the incorrect wording. I have fixed it. Let me know how it looks now. Appreciate it.

      Delete
    2. Thanks for updating.

      > Top level types can only have internal or public accessibility.

      You're still saying types when you're really talking about members. I would suggest putting types and members on an equal level. This would clarify that the modifiers have nothing to do with what the type or member is, but where it is located. For example: https://gist.github.com/jnm2/97aac384432a4b4bd518

      Delete
    3. Updated. Thanks a lot for the suggestion :) I think now it's much more clear.

      Delete
  2. Thank you for this list, I found a lot of interesting things. I also learn some advance about
    Dotnet Training in Chennai

    ReplyDelete