Private Variables in C#

The private access specifier in C# allows a class to hide its member variables and member functions from other functions and objects. Only methods of the same class can access its private members. Even an instance of a class cannot access its private members directly from outside the class.

Syntax

Following is the syntax for declaring a private variable −

private dataType variableName;

For example −

private double length;
private int count;
private string name;

Key Rules of Private Variables

  • Private members can only be accessed within the same class.

  • They are not accessible from derived classes or external classes.

  • Private variables provide data encapsulation and protect internal state.

  • Access to private variables is typically provided through public methods (getters/setters).

Private Variable Access Class Box private double length; public void setLength() External External ? No Access ? No Access ? Access ? Access Private variables need public methods for external access

Using Private Variables with Public Methods

Private variables are typically accessed through public methods that provide controlled access to the internal state −

using System;

class Box {
   private double length; // Length of a box
   private double breadth; // Breadth of a box
   private double height; // Height of a box

   public void setLength(double len) {
      length = len;
   }

   public void setBreadth(double bre) {
      breadth = bre;
   }

   public void setHeight(double hei) {
      height = hei;
   }

   public double getVolume() {
      return length * breadth * height;
   }
}

class Boxtester {
   static void Main(string[] args) {
      Box Box1 = new Box(); // Declare Box1 of type Box
      Box Box2 = new Box();
      double volume;

      // Accessing private variables outside the class gives an error.
      // Box1.length = 10; // This would cause compilation error

      Box1.setLength(6.0);
      Box1.setBreadth(7.0);
      Box1.setHeight(5.0);

      // box 2 specification
      Box2.setLength(12.0);
      Box2.setBreadth(13.0);
      Box2.setHeight(10.0);

      // volume of box 1
      volume = Box1.getVolume();
      Console.WriteLine("Volume of Box1 : {0}", volume);

      // volume of box 2
      volume = Box2.getVolume();
      Console.WriteLine("Volume of Box2 : {0}", volume);
   }
}

The output of the above code is −

Volume of Box1 : 210
Volume of Box2 : 1560

Attempting Direct Access to Private Variables

If you try to access a private variable directly from outside the class, you will get a compilation error −

using System;

class Box {
   private double length;
}

class Program {
   static void Main() {
      Box box = new Box();
      // This line will cause a compilation error
      box.length = 10.0; // Error: 'Box.length' is inaccessible due to its protection level
   }
}

The compilation error message would be −

'BoxApplication.Box.length' is inaccessible due to its protection level

Benefits of Private Variables

  • Data Encapsulation: Private variables hide internal implementation details.

  • Data Validation: Public methods can validate data before setting private variables.

  • Controlled Access: You can control how and when private data is accessed or modified.

  • Maintainability: Changes to private variables don't affect external code.

Conclusion

Private variables in C# provide data encapsulation by restricting access to class members from outside the class. They can only be accessed within the same class, typically through public methods that provide controlled access to the internal state, ensuring data integrity and security.

Updated on: 2026-03-17T07:04:35+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements