Accessibility in C#
Dec 05, 2020 04:56 0 Comments C# (C-Sharp) PARTH

                          Accessibility in C#

Accessibility means controlling the visibility of the class or its members.

Accessibilities of class and its members is controlled using Access Modifiers.

 

There are five types of access modifiers in C# -

  1. Public – It is applicable to type and member and has no access restriction.
  2. Protected – It is applicable to member and is accessible from within the class or by derived class.
  3. Internal – It is applicable to type and member and is accessible from within the files in the same assembly.
  4. Protected Internal – It is applicable to member and it is accessible from within the class or derived class and also within the derived class from another assembly.
  5. Private – It is applicable to member and is only accessible within the class.

Let’s see one example -

class Person

{

int Id;

string Name;

private string DOB;

internal string Age;

protected string FirstName;

protected string LastName;

protected internal void DisplayDetails()

{

}

public string Name

{

get { return FirstName + " " + LastName; }

}

}

Important points to be kept in mind related to access modifiers in C# -

  • Class is internal if no access modifier is provided.
  • Members are private if no access modifier is provided.
  • Private access is the least permissive access level.
  • By default, interfaces are internal.
  • Interfaces can be public or internal.
  • Access modifiers cannot be applied to interface members.
Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect