Posted by: Amit Andharia | January 3, 2009

Public Access Modifier

 

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
{

public int x; // No access restrictions.
}
// Public access
using System;
class Location
{
public int x;
public int y;
}

Output:
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.


Responses

  1. […] modifier: A keyword, such as private, protected, internal, or public, that restricts access to a type or type […]

  2. Good work.

  3. Keep up the good work. “My Experience Your Knowledge” is very impressive.


Leave a comment

Categories