AccessModifiers.md - brainchildservices/curriculum GitHub Wiki
Slide 1
C# Access Modifiers
Visibility and Accessibility Modifiers
These modifiers are used to control what levels of access you want to apply to your types, and type members such as methods and properties.
Why Access Modifiers?
To control the visibility of class members (the security level of each individual class and class member).
To achieve "Encapsulation" - which is the process of making sure that "sensitive" data is hidden from users. This is done by declaring fields as private.
Slide 2
public: The code is accessible for all classes
https://dotnetfiddle.net/lPpysl
private: The code is only accessible within the same class
https://dotnetfiddle.net/Qgi6om
protected: The code is accessible within the same class, or in a class that is inherited from that class.
https://dotnetfiddle.net/caGRTP
internal: The code is only accessible within its own assembly, but not from another assembly.
https://dotnetfiddle.net/HexZgm
protected internal: The type or member can be accessed by any code in the assembly in which it's declared, or from within a derived class in another assembly.
https://dotnetfiddle.net/z5R5gm
private protected: The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.
https://dotnetfiddle.net/9tsrXy
Slide 3
When do you get the following error? Person.address' is inaccessible due to its protection level
Ans: If we try to access a private field outside the class where it is declared, we would get this error.
Exercise:
Q1. Modify the example class
Make the class public
Add a public method named 'GetAge' that returns an int.
https://dotnetfiddle.net/CaV4mw
Slide 4
Q2. Modify the example class Person
Make the class internal
Add a private method to the example class that contains the method body "return age.ToString()".
https://dotnetfiddle.net/1U9vrl
Q3. Modify the example classes
Add a protected field to the class 'Student' named 'graduationYear' of type int.
https://dotnetfiddle.net/e8n15v
Slide 5
Q4. Modify the example class
Add an internal field to the example class called 'order' of type 'int'".
https://dotnetfiddle.net/zpgTXO
Static Modifier
--> static class: If a class is defined as static, then the class cannot be instantiated. Meaning an object of the class cannot be created using the new keyword or an instance of the class cannot be created.
- All the members of a static class have to be static.
- Members of the static class are accessed using the class name
Progam: 1> The wrong way to do it: https://dotnetfiddle.net/Sc0pdg
Progam: 2> The right way to do it: https://dotnetfiddle.net/xUQJOO
Slide 6
--> static members: Non-static class can have static members. Static members are also accessed using class name instead of object name since there is only one instance of static members. Only one copy of a static member exists, regardless of how many instances of the class are created.
Eg: The Main method in Console Application. It is defined as static because it is the starting point of the application. If there are more instances of the Main method, the program won't be able to know to where to start the execution.
Progam: 1> The wrong way to do it: https://dotnetfiddle.net/MIWGFn
Progam: 2> The right way to do it: https://dotnetfiddle.net/2Cecax
Q1. When do you get the following error?
"'Person.address': cannot declare instance members in a static class"
Ans: If we try to define a non-static member inside a static class.
Slide 7
Q2. When do you get the following error?
"Cannot create an instance of the static class 'Person'"
Ans: If we try to create an instance(object) of a static class.
Exercise:
- Modify the example class
Modify the 'newLine' field to make it static.
Add a new static method called Version to the class Environment, of return type 'int' which returns the number 4.
https://dotnetfiddle.net/llLfgS
References:
https://www.w3schools.com/cs/cs_access_modifiers.asp
https://dotnetcademy.net/Learn/2040/Pages/1
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers