Introduction
Public access specifier means, any public member can be accessed from inside and outside the class. Public access specifier allows a class to expose its member variables and functions to other functions and objects.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace public_access_specifier
{
class rectangle
{
public double length; /*Public member variable define Length and width , which can be access in
main() function, using in instance of the rectangle class , named rect. */
public double width;
public double ReturnArea() /*The member function ReturnArea() and Display() can also access
variables directly without using any instance of the class*/
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length={0}", length);
Console.WriteLine("Width={0}", width);
Console.WriteLine("Area={0}", ReturnArea());
}
}
class ExecuteArea
{
static void Main(string[] args)
{
rectangle rect = new rectangle();
rect.length = 4.5;
rect.width = 3.5;
rect.Display(); /*The member function Display() is also declare Public, so can also be accessed
from main() using in instance of the Rectangle class, named rect */
}
}
}
Output


Join the conversation! Your thoughts help the community grow.