public Home
This page is specific to:.NET Framework Version:1.12.03.03.54.0
C# Programmer's Reference
public

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.

For a comparison of public with the other access modifiers, see Accessibility Levels.

Example

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

// protected_public.cs
// Public access
using System;
class MyClass1 
{
   public int x; 
   public int y;
}

class MyClass2 
{
   public static void Main() 
   {
      MyClass1 mC = new MyClass1();

      // Direct access to public members:
      mC.x = 10;
      mC.y = 15;
      Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y); 
   }
}

Output

x = 10, y = 15

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

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

See Also

C# Keywords | Access Modifiers | Accessibility Levels | Modifiers | 3.5.1 Declared accessibility

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View