Access Modifiers






2.06/5 (9 votes)
Sep 3, 2007
3 min read

48461

434
Some Use of Access Modifiers
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).
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
.
// protected_public.cs // Public access using System; class CClass1 { public int x; public int y; } class CClass2 { public static void Main() { CClass1 CC = new CClass1(); // here is direct access to public members: CC.x = 3; CC.y = 5; Console.WriteLine("x = {0}, y = {1}", CC.x, CC.y); } }
Output
x = 3, y = 5
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; // Will give an error b.x = 7; // OK } }
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.
// protected_keyword.cs using System; class CClass { protected int a; protected int b; } class DerivedClass: CClass { public static void Main() { DerivedClass dC = new DerivedClass(); //here is direct access to protected members: dC.a = 3; dC.b = 5; Console.WriteLine("a = {0}, b = {1}", dC.a, dC.b); } }
Output
a = 3, b = 5
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.
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:
// Assembly1.cs // compile with: /target:library internal class BaseClass { public static int IntM = 0; }
File Assembly2.cs
// Assembly2.cs // compile with: /reference:Assembly1.dll // CS0122 expected class TestAccess { public static void Main() { BaseClass myBase = new BaseClass(); // error, BaseClass not visible outside assembly } }
The internal keyword is an access modifier for types and type members. Internal members are accessible only within files in the same assembly.
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()
.
// private_keyword.cs using System; class Emp { public string name = "xx"; double salary = 100.00; // private access by default public double GetSalary() { return salary; } } class MainClass { public static void Main() { Emp e = new Emp(); // Accessing the public field: string n = e.name; // Accessing the private field: double s = e.GetSalary(); } }
In the preceding example, if you attempt to access the private members directly by using a statement like this:
double s = e.salary;
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