Click here to Skip to main content
Click here to Skip to main content

Access Modifiers

, 2 Sep 2007
Rate this:
Please Sign up or sign in to vote.
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).
[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.

// 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.
[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:

// 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.

[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().

// 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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

AmitDhiman_India
Web Developer
Unknown
Working in IT/Telecom Industry, done MCP, MCA, BCA. Have aroung 4.8 Yrs of Experience, knowledge of .NET[C# & ASP.NET], Web Services, Laszlo Technology, Ajax Technology, Salesforce API Integration, Google APi's and etc.

Comments and Discussions

 
Generali have question for u !! Pinmemberram kumar 20212-Sep-07 22:52 
GeneralRe: i have question for u !! PinmemberAmitDhiman_India3-Sep-07 0:10 
GeneralThis article is copied from MSDN doc PinmemberBert delaVega3-Sep-07 3:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 3 Sep 2007
Article Copyright 2007 by AmitDhiman_India
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid