
 |
|
Microsoft® Visual C#™ .NET Language Reference
|
|
|
Author
|
|
Microsoft Corporation
|
|
|
Pages
|
404
|
|
Disk
|
N/A
|
|
Level
|
Advanced
|
|
Published
|
03/06/2002
|
|
ISBN
|
9780735615540
|
|
Price
|
$39.99
To see this book's discounted price, select a reseller below.
|
|
|
|
|
 |
|
|
C# Keywords continued
Modifiers
Modifiers are used to modify declarations of types and type members. This section introduces the C# modifiers:
| Modifier | Purpose |
Access Modifiers
public
private
internal
protected
| Specify the declared accessibility of types and type members. |
| abstract | Indicate that a class is intended only to be a base class of other classes. |
| const | Specify that the value of the field or the local variable cannot be modified. |
| event | Declare an event. |
| extern | Indicate that the method is implemented externally. |
| override | Provide a new implementation of a virtual member inherited from a base class. |
| readonly | Declare a field that can only be assigned values as part of the declaration or in a constructor in the same class. |
| sealed | Specify that a class cannot be inherited. |
| static | Declare a member that belongs to the type itself rather than to a specific object. |
| unsafe | Declare an unsafe context. |
| virtual | Declare a method or an accessor whose implementation can be changed by an overriding member in a derived class. |
| volatile | Indicate that a field can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread. |
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:
- public
- protected
- internal
- private
The following five accessibility levels can be specified using the access modifiers:
public protected internal internal protected private
This section also introduces the following topics:
- Accessibility Levels
- Accessibility Domain
- Restrictions on Using Accessibility Levels
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 accessibility | Meaning |
| public | Access is not restricted. |
| protected | Access is limited to the containing class or types derived from the containing class. |
| internal | Access is limited to the current project. |
| protected internal | Access is limited to the current project or types derived from the containing class. |
| private | Access is limited to the containing type. |
Only one access modifier is allowed for a member or type, except for the protected internal 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 of | Default member accessibility | Allowed declared accessibility of the member |
| enum | public | None |
| class | private | public
protected
internal
private
protected internal |
| interface | public | None |
| struct | private | public
internal
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.
See Also
Accessibility Domain (pg. 23), Restrictions on Using Accessibility Levels (pg. 24)
Accessibility Domain
The accessibility domain of a member specifies where, in the program sections, a member can be referenced. If the member is nested within another type, its accessibility domain is determined by both the accessibility level of the member and the accessibility domain of the immediately containing type.
The accessibility domain of a top-level type is at least the program text of the project in which it is declared. That is, the entire source files of this project. The accessibility domain of a nested type is at least the program text of the type in which it is declared. That is, the type body, including any nested types. The accessibility domain of a nested type never exceeds that of the containing type. These concepts are demonstrated in the following example.
Example
This example contains a top-level type, T1, and two nested classes, M1 and M2. The classes contain fields with different declared accessibilities. In the Main method, a comment follows each statement to indicate the accessibility domain of each member. Notice that the statements that attempt to reference the inaccessible members are commented out. If you want to see the compiler errors caused by referencing an inaccessible member, remove the comments one at a time.
// cs_Accessibility_Domain.cs using System; namespace MyNameSpace { public class T1 { public static int myPublicInt; internal static int myInternalInt; private static int myPrivateInt = 0;
public class M1 { public static int myPublicInt; internal static int myInternalInt; private static int myPrivateInt = 0; } private class M2 { public static int myPublicInt = 0; internal static int myInternalInt = 0; private static int myPrivateInt = 0; } }
public class MainClass { public static int Main() { // Access to T1 fields: T1.myPublicInt = 1; // Access is unlimited T1.myInternalInt = 2; // Accessible only in current project // T1.myPrivateInt = 3; // Error: inaccessible outside T1
// Access to the M1 fields: T1.M1.myPublicInt = 1; // Access is unlimited T1.M1.myInternalInt = 2; // Accessible only in current project // T1.M1.myPrivateInt = 3; // Error: inaccessible outside M1
// Access to the M2 fields: // T1.M2.myPublicInt = 1; // Error: inaccessible outside T1 // T1.M2.myInternalInt = 2; // Error: inaccessible outside T1 // T1.M2.myPrivateInt = 3; // Error: inaccessible outside M2
return 0; } } }
See Also
Accessibility Levels (pg. 22), Restrictions on Using Accessibility Levels (pg. 24)
Restrictions on Using Accessibility Levels
When you declare a type, it is essential to see if that type has to be at least as accessible as another member or type. For example, the direct base class must be at least as accessible as the derived class. The following declarations will result in a compiler error, because the base class BaseClass is less accessible than MyClass:
class BaseClass {...} public class MyClass: BaseClass {...} // Error
The following table summarizes the restrictions on using declared accessibility levels.
| Context | Remarks |
| Classes | The direct base class of a class type must be at least as accessible as the class type itself. |
| Interfaces | The explicit base interfaces of an interface type must be at least as accessible as the interface type itself. |
| Delegates | The return type and parameter types of a delegate type must be at least as accessible as the delegate type itself. |
| Constants | The type of a constant must be at least as accessible as the constant itself. |
| Fields | The type of a field must be at least as accessible as the field itself. |
| Methods | The return type and parameter types of a method must be at least as accessible as the method itself. |
| Properties | The type of a property must be at least as accessible as the property itself. |
| Events | The type of an event must be at least as accessible as the event itself. |
| Indexers | The type and parameter types of an indexer must be at least as accessible as the indexer itself. |
| Operators | The return type and parameter types of an operator must be at least as accessible as the operator itself. |
| Constructors | The parameter types of a constructor must be at least as accessible as the constructor itself. |
Example
The following example contains erroneous declarations of different types. The comment following each declaration indicates the expected compiler error.
// Restrictions_on_Using_Accessibility_Levels.cs // CS0052 expected as well as CS0053, CS0056, and CS0057 // To make the program work, change access level of both class B // and MyPrivateMethod() to public.
using System;
// A delegate: delegate int MyDelegate();
class B { // A private method: static int MyPrivateMethod() { return 0; } } public class A { // Fields: public B myField = new B(); // Error: The type B is less accessible // than the field A.myField. // Constants: public readonly B myConst = new B(); // Error: The type B is less accessible // than the constant A.myConst.
// Methods: public B MyMethod() { return new B(); // Error: The type B is less accessible } // than the method A.MyMethod.
// Properties: public B MyProp { set { } } // Error: The type B is less accessible than the property A.MyProp
// Delegates: MyDelegate d = new MyDelegate(B.MyPrivateMethod); // Even when B is declared public, you still get the error: // "The parameter B.MyPrivateMethod is not accessible due to // protection level." // Operators: public static B operator + (A m1, B m2) { return new B(); // Error: The type B is less accessible // than the operator A.operator +(A,B) } static void Main() { Console.Write("Compiled successfully"); } }
See Also
Accessibility Domain (pg. 23), Accessibility Levels (pg. 22)
Previous
| Table of Contents
| Next
Last Updated: February 19, 2002
|