C# Access Modifiers: What Why How | C#.Net Tutorial - GTech eAcademy

 Watch Full Explanation on our YouTube channel  

https://www.youtube.com/watch?v=4Q8z6qUJN3Y&t=234s


C# Access Modifiers: What Why How | C#.Net Tutorial - GTech eAcademy

Discussion points

1) What is Assembly in .Net?
2) What is Access Modifier?
3) Why do we need to control access to a class or its members?
4) How to implement with Example?
5) Types of Access Modifier & their explanation
6) Interview Questions

What is Assembly in .Net?

1) An assembly is a compiled code library (.dll/.exe) used for deployment.

What is Assembly, exe, dll in .Net - GTech eAcademy
Assembly in .Net

2) Assemblies take the form of executable (.EXE) or dynamic link library (.DLL) files and are the building blocks of .NET applications.
3) Assembly is an OS and H/W independent code.

What is Access Modifier?

A way to control access to a class or its members.
OR
Access Modifiers are keywords that define the accessibility of a class, data member or member function in a program.

Why do we need to control access to a class or its members?

Restrict unwanted data manipulation by external programs or classes.
OR
To create safety in our programs to improve the robustness. So the developers will not accidentally create bugs in code.

How to implement Access Modifiers?

In C language, everything is public. So that data moves freely in the whole program. In Object-Oriented programming, one concept was introduced i.e. Access Modifier. Using this concept we can control access to everything like classes, class variables & member functions.

In below image:
i) A Student class containing data member (or, class variable) i.e.
private string name;
   Here 'private' is an Access Modifier keyword that states that private variable can be accessed by their own class only.

How to implement C# Access Modifier - GTech eAcademy
How to implement Access Modifierin C# 

ii) As you can see, A different class (i.e. Demo class) is trying to access a private variable 'name' of Student class. But the compiler will throw an error "Inaccessible due to its protection level".
iii) This is the power of Access Modifier that you can control access to anything.

Types of Access Modifier

There are 4 access modifiers (public, private, protected, internal) which define the 6 accessibility levels as follows:
1)   public
2)   private
3)   internal
4)   protected
5)   protected internal
6)   private protected

1) public Accessibility Level

Access is granted to the entire program
i) Within Same Class

Public Access Modifier - GTech eAcademy
public Accessibility Level

ii) Any Class in Current Assembly
iii) Any Class in Outside Assembly

2) private Accessibility Level

Access is granted within the same class only

Private Access Modifier - GTech eAcademy
private Accessibility Level

3) internal Accessibility Level

Access is granted
i) Within Same Class

Internal Access Modifier - GTech eAcademy
internal Accessibility Level

ii) Any Class in Current Assembly

4) protected Accessibility Level

Access is granted
i) Within Same Class

Protected Access Modifier - GTech eAcademy
protected Accessibility Level

ii) Only Derived Class in Current Assembly
iii) Only Derived Class in Outside Assembly

5) private protected Accessibility Level

Access is granted
i) Within Same Class

Private Protected Access Modifier - GTech eAcademy
private protected Accessibility Level

ii) Only Derived Class in Current Assembly

6) protected internal Accessibility Level

Access is granted
i) Within Same Class

Protected Internal Access Modifier - GTech eAcademy
protected internal Accessibility Level

ii) Any Class in Current Assembly
iii) Only Derived Class in Outside Assembly

Interview Questions

1) What are different access modifiers? 2) What are Access Modifiers used for? Ans. Access Modifiers are used to control access to a class or its members. 3) Why do we need to control access to a class or its members? Ans. Restrict unwanted data manipulation by external programs or classes.


***********************************************************************************

Our main focus is to provide free e-learning videos related to programming languages.

Please SUBSCRIBE & Share to support us 😀😀


- Ankush Garg

- GTech eAcademy (Technology can be fun) 

Follow us on Facebook

#GTecheAcademy #CsharpAccessModifiers #AccessModifiersInCsharp #AccessModifiers #AccessSpecifiers #CsharpDotNetTutorial #InternalAccessModifier #ProtectedInternalAccessModifier #PrivateProtectedAccessModifiers #CSharpTutorial #GtechAcademy

Comments

Popular posts from this blog

C# Namespace with real life example - GTech eAcademy