Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


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.


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); 


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

© 2015 Microsoft