Introduction
Access modifiers are keywords used to specify the declared accessibility of a member or a type. This section introduces the four access modifiers:
- public (No restrictions to access).
- protected (Access is limited to within the class definition and any class that inherits from the class).
- internal (Access is limited exclusively to classes defined within the current project assembly).
- private (Access is limited to within the class definition; This is the default access modifier type if none is formally specified).
[public]
(No restrictions to access - methods are open for anyone to see)
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.
Example
In the following example, two classes are declared, CClass1
and CClass2
. The public members x and y of the CClass1
are accessed directly from CClass2
.
using System;
class CClass1
{
public int x;
public int y;
}
class CClass2
{
public static void Main()
{
CClass1 CC = new CClass1();
CC.x = 3;
CC.y = 5;
Console.WriteLine("x = {0}, y = {1}", CC.x, CC.y);
}
}
Output
If you want to change the public access level to private or protected, you will get the error message:
'CClass1.y' is inaccessible due to its protection level.
[protected]The protected keyword is a member access modifier.
A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.
A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type. For example, consider the following code segment:
class XX
{
protected int x = 111;
}
class YY : XX
{
void F()
{
XX a = new XX();
YY b = new YY();
a.x = 7;
b.x = 7;
}
}
The statement a.x=7
generates an error because XX is not derived from YY.
Note: "Struct members cannot be protected because the struct cannot be inherited."
It is an error to reference a protected member from a class, which is not derived from the protected member's class.
Example
In this example, the class DerivedClass
is derived from CClass
; therefore, you can access the protected members of the base class directly from the derived class.
using System;
class CClass
{
protected int a;
protected int b;
}
class DerivedClass: CClass
{
public static void Main()
{
DerivedClass dC = new DerivedClass();
dC.a = 3;
dC.b = 5;
Console.WriteLine("a = {0}, b = {1}", dC.a, dC.b);
}
}
Output
If you change the access levels of x
and y
to private, the compiler will issue the error messages:
'CClass.a' is inaccessible due to its protection level.
'CClass.b' is inaccessible due to its protection level.
[internal]
A common use of internal access is in component-based development because it enables a group of components to cooperate in a private manner without being exposed to the rest of the application code. For example, a framework for building graphical user interfaces could provide Control and Form classes that cooperate using members with internal access. Since these members are internal, they are not exposed to code that is using the framework.
It is an error to reference a member with internal access outside the assembly within which it was defined.
Note: An internal virtual method can be overridden in some languages, such as textual Microsoft intermediate language (MSIL) using Ilasm.exe, even though it cannot be overridden using C#.
Example
This example contains two files, Assembly1.cs
and Assembly2.cs
. The first file contains an internal base class, BaseClass
. In the second file, an attempt to access the member of the base class will produce an error.
File Assembly1.cs:
File Assembly2.cs
class TestAccess
{
public static void Main()
{
BaseClass myBase = new BaseClass();
}
}
The internal keyword is an access modifier for types and type members. Internal members are accessible only within files in the same assembly.
[private]- (Access is limited to within the class definition)
The private keyword is a member access modifier. Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared.
Nested types in the same body can also access those private members.
It is a compile-time error to reference a private member outside the class or the struct in which it is declared.
Example
In this example, the Emp
class contains a public member, Name
, and a private member, Salary
. The public member can be accessed directly, while the private member must be accessed through the public method GetSalary()
.
using System;
class Emp
{
public string name = "xx";
double salary = 100.00;
public double GetSalary() {
return salary;
}
}
class MainClass
{
public static void Main()
{
Emp e = new Emp();
string n = e.name;
double s = e.GetSalary();
}
}
In the preceding example, if you attempt to access the private members directly by using a statement like this:
you will get the error message:
'Emp.Salary' is inaccessible due to its protection level.
Points of Interest
The following five accessibility levels can be specified using the access modifiers:
public, protected, internal, internal protected, private