find and solve || findandsolve.com
Please wait.....

Access Modifiers in C#


In C#, access modifiers are keywords that determine the accessibility of a type or member. There are five access modifiers in C#:

public: The type or member can be accessed from anywhere.
private: The type or member can only be accessed from within the same class or struct.
protected: The type or member can only be accessed from within the same class or struct, or from a derived class.
internal: The type or member can only be accessed from within the same assembly (a collection of classes and other resources that are compiled together).
protected internal: The type or member can be accessed from within the same assembly, or from a derived class in another assembly.

Here is an example of how access modifiers can be used in a C# class:

public class MyClass
{
    private int _privateField;
    protected int _protectedField;
    public int PublicField;
    private void PrivateMethod() { }
    protected void ProtectedMethod() { }
    public void PublicMethod() { }
}

In this example, the _privateField and PrivateMethod() members are only accessible from within the MyClass class, the _protectedField and ProtectedMethod() members are accessible from within MyClass and derived classes, and the PublicField and PublicMethod() members are accessible from anywhere.

Access modifiers in C# are used to specify the scope of accessibility of a member of a class or type of the class itself. For example, a public class is accessible to everyone without any restrictions. Access modifiers are used to implement the encapsulation of OOP. Access modifiers allow you to define who does or who doesn't have access to certain features.


TYPES OF MODIFIER AND THEIR EXAMPLES:


C# using public modifier:

The public keyword is an access modifier, used to set the access level/visibility for classes, fields, methods, and properties.

Example:

class Car
{
public string model = "Mustang";
  {
    class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.model);
}
}
The output:
Mustang
C# using private modifier:
If you declare a field with a private access modifier, it can only be accessed within the same class:
Example:
class Car
{
  private string model = "Mustang";
  static void Main(string[] args)
  {
    Car myObj = new Car();
    Console.WriteLine(myObj.model);
  }
}
The output:
Mustang

C# using protected modifier:

A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.
Example:
     class User
     {
         protected string Name;
        {
              Console.WriteLine("Name: {0}", Name);
         }
     }
     class Program
     {
         static void Main(string[] args)
         {
             User u = new User();
             u.Name = "Suresh Dasari";
             u.GetUserDetails();
             Console.WriteLine("\nPress Enter Key to Exit..");
             Console.ReadLine();
         }
     } 

The output:
Suresh Dasari

Related information

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

If findandsolve.com felt valuable to you, feel free to share it.

Comments



Report Response