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
{
using System;
class Location
{
public int x;
public int y;
public int y;
}
Output:
x = 10, y = 15If you change the public access level to private or protected, you will get the error message:
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.
[…] modifier: A keyword, such as private, protected, internal, or public, that restricts access to a type or type […]
By: C# Keywords & Terminology « Journey through Web Development on January 5, 2009
at 8:21 am
Good work.
By: Syed Tayyab Ali on January 16, 2009
at 9:11 pm
Keep up the good work. “My Experience Your Knowledge” is very impressive.
By: cavemansblog on February 28, 2009
at 7:04 pm