[C#] C# Access Modifiers

Date:     Updated:

Categories:

Tags:

📋 This is my note-taking from what I learned in the class “Programming 2”


Access Modifiers

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

C# has the following access modifiers:

Modifier Description
public The code is accessible for all classes.
private The code is only accessible within the same class.
protected The code is accessible within the same class, or in a class that is inherited from that class.
internal The code is only accessible within its own assembly, but not from another assembly.

There’s also two combinations: protected internal and private protected.


Private Modifier

If you declare a field with a private access modifier, it can only be accessed within the same class:

class Car
{
  private string model = "Mustang";

  static void Main(string[] args)
  {
    Car myObj = new Car();
    Console.WriteLine(myObj.model);
  }
}

// Output: Mustang

If you try to access it outside the class, an error will occur:

class Car
{
  private string model = "Mustang";
}

class Program
{
  static void Main(string[] args)
  {
    Car myObj = new Car();
    Console.WriteLine(myObj.model);
  }
}

// Error: 'Car.model' is inaccessible due to its protection level
The field 'Car.model' is assigned but its value is never used


Public Modifier

If you declare a field with a public access modifier, it is accessible for all classes:

class Car
{
  public string model = "Mustang";
}

class Program
{
  static void Main(string[] args)
  {
    Car myObj = new Car();
    Console.WriteLine(myObj.model);
  }
}

// Output: Mustang

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.

By default, all members of a class are private if you don’t specify an access modifier:

class Car
{
  string model;  // private
  string year;   // private
}


this Keyword

this keyword is used to refer to the current instance of the class. It is used to access members from the constructors, instance methods, and instance accessors. this keyword is also used to track the instance which is invoked to perform some calculation or further processing related to that instance.

Using ‘this’ keyword to refer current class instance members:

using System;

namespace geeksForGeeks
{
  class Geeks
  {
    // Instance member
    public string Name;

    public string GetName()
    {
      return Name;
    }

    public void SetName(string Name)
    {
      // Referring to current instance: 'this.Name' refers to class member
      this.Name = Name;
    }
  }

  class Program
  {
    // Main method
    public static void Main()
    {
      Geeks obj = new Geeks();
      obj.SetName("Geeksforgeeks);

      Console.WriteLine(obj.GetName());
    }
  }
}

Using this() to invoke the constructor in same class:

using System;

namespace geeksforgeeks
{
  class Geeks
  {
    // Calling another constructor: calls Geeks(string Name) before Geeks()
    public Geeks(): this("geeks")
    {
      Console.WriteLine("Non-Parameter Constructor Called");
    }

    // Declare Parameter Constructor
    public Geeks(string Name)
    {
      Console.WriteLine("Parameter Constructor Called");
    }
  }

  class Program
  {
    // Main method
    public static void Main()
    {
      Geeks obj = new Geeks(); // Warning: obj never used
    }
  }
}

Using ‘this’ keyword to invoke current class method:

using System;

class Test
{
  void display()
  {
    // Calling function show()
    this.show();

    Console.WriteLine("Inside display function");
  }

  void show()
  {
    Console.WriteLine("Inside show function");
  }

  // Main method
  public static void Main(string[] args)
  {
    Test t1 = new Test();
    t1.display();
  }
}

Using ‘this’ keyword as method parameter:

using System;

class Test
{
  int a;
  int b;

  // Default constructor
  Test ()
  {
    a = 10;
    b = 20;
  }

  // Method that receives 'this' keyword as parameter
  void display(Test obj)
  {
    Console.WriteLine("a = " + a + "b = " + b);
  }

  // Method that returns current class instance
  void get()
  {
    display(this);
  }

  // Main method
  public static void Main(string[] args)
  {
    Test obj = new Test();
    obj.get();
  }
}




Back to Top

See other articles in Category CS

Leave a comment