Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
C# Reference
C# Keywords
 public
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
C# Language Reference
public (C# Reference)

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.
}

See Access Modifiers (C# Programming Guide) and Accessibility Levels (C# Reference) for more information.

In the following example, two classes are declared, Point and MainClass. The public members x and y of Point are accessed directly from MainClass.

C#
class PointTest
{
    public int x; 
    public int y;
}

class MainClass4
{
    static void Main() 
    {
        PointTest p = new PointTest();
        // Direct access to public members:
        p.x = 10;
        p.y = 15;
        Console.WriteLine("x = {0}, y = {1}", p.x, p.y); 
    }
}
// Output: x = 10, y = 15

If you change the public access level to private or protected, you will get the error message:

'Point.y' is inaccessible due to its protection level.

For more information, see the following sections in the C# Language Specification:

  • 3.5.1 Declared Accessibility

  • 3.5.4 Accessibility constraints

  • 10.3.5 Access Modifiers

  • 10.3.8.2 Declared Accessibility

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker