Public Access Modifier in C#
In this article I will explain public access modifier in C#.
Public is the most common access specifier in C#. The public keyword is an access modifier for types and type members. It is quite clear, the "public" access modifier achieves the highest level of accessibility. If you define your class properties and methods as "public", then it can be used anywhere in your C# program.
Can be accessed by objects of the class.
Can be accessed by derived classes.
// String Variable declared as public
public string name;
// Public method
public void print()
Console.WriteLine("\nMy name is " + name);
static void Main(string args)
accessmod nam = new accessmod();
Console.Write("Enter your name: ");
// Accepting value in public variable that is outside the class
nam.name = Console.ReadLine();
In the following example, the variable and method of accessmod is set to be "public", and outside the class, an instance is created of accessmod. Then the "print()" method and variable is accessed by nam .
The output of following program