Access Specifiers

In this chapter you will learn:

  1. Get to know C#'s Access Specifiers
  2. Matrix showing the usage of access modifiers
  3. A demo showing how to use access modifiers

C#'s Access Specifiers

An access specifier determines how other parts of a program can access a class member.

Member access is controled by four access specifiers:

Access modifierMeaning
publicAccessible anywhere;
internalAccessible only within its assembly or friend assemblies;
privateVisible only within its own type;
protectedVisible only within containing type or subclasses
protected internalThe union of protected and internal accessibility

interal is the default for non-nested types. public is implicit for members of an enum or interface type. private is the default for members of a class or struct.

Matrix showing the usage of access modifiers

Visibility modifiers indicate which other code items can view an item.

Modifier Applies To Description
public Any types or members visible to all .
protectedAny member of a type, also any nested type visible only to all derived type.
internal Any member of a type, also any nested type visible only within its containing assembly.
private Any types or members visible only inside the type to which it belongs.
protected internal Any member of a type, also any nested type visible to any code within its containing assembly and also to any code inside a derived type.

Demo for access modifiers

class MyClass/* j av a2  s.  c  o  m*/
{
  public             string publicStringField;
  protected internal string protectedInternalStringField;
  internal           string internalStringField;
  protected          int protectedField = 150;
  private            int privateField;

  public void setPrivateField(int privateField)
  {
    this.privateField = privateField;
  }

  public int getPrivateField()
  {
    return privateField;
  }

  public void Start()
  {
    System.Console.WriteLine("Starting MyClass ...");
    privateMethod();
    System.Console.WriteLine("MyClass started");
  }

  private void privateMethod()
  {
    System.Console.WriteLine("Turning starter motor ...");
  }

}


class MainClass
{
  public static void Main()
  {
    MyClass myMyClass = new MyClass();

    myMyClass.publicStringField = "Toyota";
    myMyClass.protectedInternalStringField = "MR2";
    myMyClass.internalStringField = "black";

    myMyClass.setPrivateField(1995);

    System.Console.WriteLine("myMyClass.publicStringField = " + myMyClass.publicStringField);
    System.Console.WriteLine("myMyClass.protectedInternalStringField = " + myMyClass.protectedInternalStringField);
    System.Console.WriteLine("myMyClass.internalStringField = " + myMyClass.internalStringField);

    System.Console.WriteLine("myMyClass.getPrivateField() = " + myMyClass.getPrivateField());

    myMyClass.Start();
  }
}

Next chapter...

What you will learn in the next chapter:

  1. Use Properties to get and set private member variable
  2. private static and const Fields
  3. Using Methods to change private fields
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances