Visibility and Accessibility Modifiers

Public Access Modifier

Access modifiers are the most commonly used modifiers in most C# program code, and you've already used at least one of them. The access modifiers are the keywords publicprivateinternal and protected. These modifiers are used to control what levels of access you want to apply to your types, and type members such as methods and properties. Let's have a look at these modifiers.

using System; public class Program...

 

public modifier

The modifier keyword public is used to mark a type (such as a class), or a type member (such as a method) as publicly accessible - this means that it can be called from external callers. The actually meaning of this has two different contexts, types and type members.

From the context of a type - the external callers are those outside of your current program assembly. So, if you had created a class named Program like the following:

public class Program
{

}

This class is accessible from any assembly (another program or library) that references your program.

From the context of a type member, the external callers are those outside of your current program assembly, and also other code in your own program assembly, for example given the following class:

public class Program
{
    public void SayHello()
    {
        Console.WriteLine("Hello");
    }
}

The method SayHello can be called from any external assembly, and any code in your current program.

Page 2 of 18