We will explain the reasons for access modifiers in c#.
What are Access Modifiers?
There are levels of access you can specify for specific constructs. The only ones that you can’t be have a access modifier is based on their accessibility domain. How you would know their accessibility level would be based on the member accessibility level, and containing type accessibility level. Never can the nested type have more access than the type itself.
The only constructs that can’t have a access modifier, and have to go by their default access modifier. Are interfaces, namespaces, and enums. While structs can only have public, internal, and private access modifiers. Then classes can have all access modifiers.
We will have all our properties in our PublicClass.cs file. It will contain 5 properties all varying in access levels.
A private Name1 property which would have the least amount of access, only can be accessed within the class.
A protected Name2 property which would have the second least amount of access, only can be accessed from a derived class.
A internal Name3 property which would have the third least amount of access, only can be accessed within it’s current assembly or namespace.
A public Name4 property which would have the most amount of access, it can be access through out the application.
Then a protected internal Name5 property which we would combine two access modifiers. It can be accessed in the current assembly or namespace, and can be access in a derived class.
We would define all these properties within our constructor which take the corresponding properties lowercase as arguments, all would be strings. Then we would assign those argument to our properties.
Now in your Program.cs file within you Main method, create a new instance of your PublicClass, and assign it to a variable called exampleClass.
The private access modifier will give your member of your class or variable the least amount of access. When your try it to log into the console it will give you an error. You would use the private access modifier when implementing logic for your getter’s and setters, by assigning it to a variable and manipulating that variable within your setter, and returning it in your getter.
The protected access modifier it will give your member of your class or variable the second least amount of access. It can only be accessed from a derived class or a inherited class. When your try to log into the console, it will give you a error.
The internal access modifier will give your member of your class or variable that third least amount of access. It can be only accessed from the same namespace. When your try to log into the console, it will log it successfully, because the PublicClass is in the same namespace. Used in component based development, primarily as a static value.
The public access modifier gives your member, and property the most access. Therefore can be accessed throughout your application.
You can also combine access modifiers, for example you can have your protected and internal access modifier combined. Which means it can be accessed within the class or the class inherited or the namespace. When you log into the console, it would work.
Well those are a couple of examples.
Here is my github repo for reference.
A Console App using Access Modifiers. Contribute to AliA1997/C-Access-Modifiers development by creating an account on…