Wednesday, October 27, 2010

Modifiers in .NET

Access Modifiers

Access modifiers are keywords used to specify the declared accessibility of a member or a type. This section introduces the four access modifiers:

The following five accessibility levels can be specified using the access modifiers:
   public   protected   internal   internalprotected   private
This section also introduces the following topics:

Accessibility Levels

When access is allowed to a member, it said to be accessible. Otherwise, it is inaccessible. Use the access modifiers, public, protected, internal, or private, to specify one of the following declared accessibilities for members.
Declared accessibilityMeaning
publicAccess is not restricted.
protectedAccess is limited to the containing class or types derived from the containing class.
internalAccess is limited to the current assembly.
protected internalAccess is limited to the current assembly or types derived from the containing class.
privateAccess is limited to the containing type.
Only one access modifier is allowed for a member or type, except for the protectedinternal combination.
Access modifiers are not allowed on namespaces. Namespaces have no access restrictions.
Depending on the context in which a member declaration takes place, only certain declared accessibilities are permitted. If no access modifier is specified in a member declaration, a default accessibility is used.
Top-level types, which are not nested into other types, can only have internal or public accessibility. The default accessibility for these types is internal.
Nested types, which are members of other types, can have declared accessibilities as indicated in the following table.

Members ofDefault member accessibilityAllowed declared accessibility of the member
enumpublicNone
classprivatepublicprotected
internal
private
protected internal
interfacepublicNone
structprivatepublicinternal
private
The accessibility of a nested type depends on its accessibility domain, which is determined by both the declared accessibility of the member and the accessibility domain of the immediately containing type. However, the accessibility domain of a nested type cannot exceed that of the containing type.

 public

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.
For a comparison of public with the other access modifiers, see Accessibility Levels.

Example

In the following example, two classes are declared, MyClass1 and MyClass2. The public members x and y of the MyClass1 are accessed directly from MyClass2.

// protected_public.cs
// Public access
using System;
class MyClass1 
{
   public int x; 
   public int y;
}

class MyClass2 
{
   public static void Main() 
   {
      MyClass1 mC = new MyClass1();

      // Direct access to public members:
      mC.x = 10;
      mC.y = 15;
      Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y); 
   }
}

Output.

x = 10, y = 15
If you change the public access level to private or protected, you will get the error message:
'MyClass1.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 A 
{
   protected int x = 123;
}

class B : A 
{
   void F() 
   {
      A a = new A();  
      B b = new B();  
      a.x = 10;   // Error
      b.x = 10;   // OK
   }
}
The statement a.x =10 generates an error because A is not derived from B.
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.
For more information on protected members, see 3.5.3 Protected access for instance members.
For a comparison of protected with the other access modifiers, see Accessibility Levels.

Example

In this example, the class MyDerivedC is derived from MyClass; therefore, you can access the protected members of the base class directly from the derived class.
// protected_keyword.cs
using System;
class MyClass 
{
   protected int x; 
   protected int y;
}

class MyDerivedC: MyClass 
{
   public static void Main() 
   {
      MyDerivedC mC = new MyDerivedC();

      // Direct access to protected members:
      mC.x = 10;
      mC.y = 15;
      Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y); 
   }
}

Output

x = 10, y = 15
If you change the access levels of x and y to private, the compiler will issue the error messages:
 
'MyClass.y' is inaccessible due to its protection level.
'MyClass.x' is inaccessible due to its protection level.

internal

The internal keyword is an access modifier for types and type members. Internal members are accessible only within files in the same assembly. For more information on assemblies, see Components and Assemblies.
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.
Caution   An internalvirtual 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#.
For a comparison of internal with the other access modifiers, see Accessibility Levels.

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

private

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.
For a comparison of private with the other access modifiers, see Accessibility Levels.

Example

In this example, the Employee 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 AccessSalary().
// private_keyword.cs
using System;
class Employee 
{
   public string name = "xx";
   double salary = 100.00;   // private access by default
   public double AccessSalary() {
      return salary;
   }
}

class MainClass 
{
   public static void Main() 
   {
      Employee e = new Employee();

      // Accessing the public field:
      string n = e.name; 
      
      // Accessing the private field:
      double s = e.AccessSalary();    
   }
}
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:
'Employee.Salary' is inaccessible due to its protection level.

 

 

 

 

 


 


 

No comments:

Post a Comment