The public keyword is an access modifier for types and type members. Public access is the most permissive access level. There are no restrictions on accessing public members, as in this example:
class SampleClass
{
using System;
class Location
{
public int y;
x = 10, y = 15If you change the public access level to private or protected, you will get the error message:
‘Location.y’ is inaccessible due to its protection level.
class MainClass
{
Locationl = new Location();
// Direct access to public members:
l.x = 10;
l.y = 15;
Console.WriteLine(“x = {0}, y = {1}”, l.x, l.y);
static void Main(){}
}
Example:
In the following example, two classes are declared, Location and MainClass. The public members x and y of Location are accessed directly from MainClass.