C# Access modifiers

PremKumarBadri 1,061 views 11 slides Apr 30, 2019
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

What Are Access Modifiers ?

Access modifiers are keywords used to specify the declared accessibility of a member or a type.

Access modifiers support the concept of encapsulation, which promotes the idea of hiding functionality.

Access modifiers allow you to define who does or doesn't have acc...


Slide Content

Access modifiers

What Are Access Modifiers ? Access modifiers are keywords used to specify the declared accessibility of a member or a type. Access modifiers support the concept of encapsulation, which promotes the idea of hiding functionality. Access modifiers allow you to define who does or doesn't have access to certain features.

Types Of Access Modifiers public protected internal private

Accessibility Levels public protected internal protected  internal private

public Public access is the most permissive access level. There are no restrictions on accessing public members. class SampleClass { public int x; // No access restrictions. }

protected A protected member is accessible within its class and by derived class instances. class A { protected  int x = 123; } class B : A { static void Main() { B b = new B(); b.x = 10; // Accessible, because this class derives from A. } }

internal Internal types or members are accessible only within files in the same assembly. public class BaseClass { internal static int x = 0; } A common use of internal access is in component-based development.

protected internal Access is limited to the current assembly or types derived from the containing class. Only one access modifier is allowed for a member or type, except when you use the protected internal combination. You can't declare a class as protected internal explicitly.

private Private access is the least permissive access level. class Employee { private int i ; // private access by default } Nested types in the same body can also access those private members.

More about Access Modifiers Access modifiers are not allowed on namespaces. If no access modifier is specified in a member declaration, a default accessibility is used. Top-level types, which are not nested in other types, can only have internal or public accessibility. The default accessibility for these types is internal.

Thank You !