jay's old blog

this blog will be deleted soon - please visit my new blog - https://thesanguinetechtrainer.com

Access Modifiers

Without access modifiers, most of the magic of encapsulation would work. A class without properly used access modifiers (or an assembly or a method) is like a house without locks. Anybody can do anything and before you know thing, things are going down the drain.

So, there are a total of five access modifiers. Here is a quick list and what they do.

Public

This means, anybody can use it. It cannot any more simpler than this. Does this mean, another class can just access it, perhaps create issues again. For instance, methods are usually public. So, that would create some problems, you would think. The thing is, methods can be called, but the internals of a method are still under your control.

That means, just because someone can access some members of your type, does not mean bad things will happen. It just means that you give them access to the extent they require.

Internal

This means, the given member is only available in the current assembly.

Okay, so assemblies. Yeah, if you are only beginning to learn programming (which is probably the case if you are reading my blog in its dictated sequence), you don’t need to know about that. However, just remember that when you prefix it, you can only use these within the assembly.

Protected

This keyword helps you restrict the usage to the 'containing class' and 'derived classes'. That means, only the members of said class can use it and those that are derived from said class.

Protected Internal

A combination of the last two. This means, either of the above two. Within assembly or derived.

Private

Usage restricted to containing class.

I don’t have a sample code for this because the stuff is self explanatory.

Follow me on twitter, facebook and instagram for more updates. Thanks!

Encapsulation and Access Modifiers

If you have been following our tutorials and associated code, you would have noticed that we have frequently used words such as 'private', 'public' and rarely, 'protected'. You probably already have a vague idea what these things do but for the sake of completion, I am including that here.  


The important concept is what these things are doing, and that is encapsulation. A capsule is something that keeps its contents separate from the outside world. It also allows the capsule's contents to be consumed in a certain way. Let's take the example of a medical capsule. It's like a container, and inside the container are its actual contents.  


There is no way for you to access the contents of the capsule (without breaking it but that is not how its meant to be used) without swallowing it. Once you swallow the capsule, the outer covering will dissolve in the stomach, allowing for the medicines to be released. Thanks to this 'encapsulation' the contents of the capsule has been delivered just the way it was meant.  


That is also what encapsulation does in c sharp (and any other object oriented programming language for that matter), allowing you to hide stuff that should be completely hidden. Further, you get to choose how outside elements can access what's inside. Which means, you can control how the types (and the objects) designed by you will behave, no matter where they are used.  


Another example I can think of is planning a party or a festival at home. It is your home where you are hosting the party or festival. You are going to invite a lot of people into your home. At the same time, just because you invited people, does not mean you want them to roam around in every part of your home. There are some places where you don’t mind them going, and other places they are forbidden. Some friends will be able to use special rooms while some can only sit in the main hall.  


Every type that you define is like this party or festival. You want your types to be used, but you decide who gets to use how much and to what extent. That is what encapsulation is all about. To do this, c sharp provides you with many 'access modifiers'. Think of access modifiers like those security guards in a museum. Different types of security guards allow for different types of access. For instance, you may have seen the guards at ATMs (who don't do anything, but that is of course part of their job) while then there are guards who protect movie stars and government folks who can do lots of things.  


In c sharp, depending on how completely open or completed closed or (as is normally the case) something in between, you want to access to be, you decide which access modifiers to use. 


Continue on to our next blog where you can find out more about access modifiers along with sample code.


Follow me on twitter, facebook and instagram for more updates. Thanks!