How To Access Private Variables of a Class In another class in C#

We can access private variable of a class in a different class in so many ways. Here is some of them:

  1. By using Public Method

    We can access a private variable in a different class by putting that variable with in a Public method and calling that method from another class by creating object of that class.

    Example:
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. namespace ConsoleApplication1  
    6. {  
    7.     class PrivateVariable  
    8.     {  
    9.         private int i = 10; //Declaring and initializing a private variable  
    10.         public void DisplayVariable()  
    11.         {  
    12.             Console.Write("The Value of Private Variable=" + i); //Accessing Private variable with in a public methode  
    13.         }  
    14.     }  
    15.     class DisplayPrivateVariable  
    16.     {  
    17.         static void Main()  
    18.         {  
    19.             PrivateVariable objPrivateVariable = new PrivateVariable();  
    20.             objPrivateVariable.DisplayVariable(); //Calling the public method  
    21.         }  
    22.     }  
  2. By Using Inner class

    By using inner class also we can access a private variable of a class in another class. For better understanding have look at the example.

    Example:
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. namespace ConsoleApplication1  
    6. {  
    7.     class PrivateVariable2  
    8.     {  
    9.         class Outerclass  
    10.         {  
    11.             private int i = 10;  
    12.             private int j = 20;  
    13.             class Innerclass  
    14.             {  
    15.                 static void Main()  
    16.                 {  
    17.                     Outerclass objouter = new Outerclass();  
    18.                     int Result = objouter.i + objouter.j;  
    19.                     Console.Write("Sum=" + Result);  
    20.                     Console.ReadLine();  
    21.                 }  
    22.             }  
    23.         }  
    24.     }  
    25. }  
  3. By Using Properties

    By using properties also we can do the same work as previous.

    Example:
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. namespace ConsoleApplication1  
    6. {  
    7.     class Employee  
    8.     {  
    9.         private int _EmpID = 1001;  
    10.         private string _EmpName;  
    11.         public int EmpID  
    12.         {  
    13.             get  
    14.             {  
    15.                 return _EmpID;  
    16.             }  
    17.         }  
    18.         public string EmpName  
    19.         {  
    20.             get  
    21.             {  
    22.                 return _EmpName;  
    23.             }  
    24.             set  
    25.             {  
    26.                 _EmpName = "Smith";  
    27.             }  
    28.         }  
    29.     }  
    30.     class AcessEmployee  
    31.     {  
    32.         static void Main()  
    33.         {  
    34.             Employee objEmployee = new Employee();  
    35.             Console.WriteLine("Employee ID: " + objEmployee.EmpID);  
    36.             Console.WriteLine("Employee old Name: " + objEmployee.EmpName);  
    37.             objEmployee.EmpName = "Dyne Smith";  
    38.             Console.WriteLine("Employee New Name: " + objEmployee.EmpName);  
    39.             Console.ReadLine();  
    40.         }  
    41.     }  
    42. }  
Ebook Download
View all
Learn
View all