|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionAccess 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 - 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.
ExampleIn the following example, two classes are declared, // 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); } } Outputx = 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 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 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 // 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); } } Outputa = 3, b = 5 If you change the access levels of '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#. ExampleThis example contains two files, 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. ExampleIn this example, the // 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 InterestThe following five accessibility levels can be specified using the access modifiers:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||