Using Access Modifiers

As mentioned at the beginning of the chapter, encapsulation , the capability to hide data and methods to stop them from cluttering up the rest of your code, is one of the biggest advantages of OOP. Encapsulating data and methods not only ensures that they don't clutter up the rest of your code, it also ensures that the rest of your code doesn't interfere with them. You can use access modifiers to set the allowed access to not only classes, but also to all members of those classes. Here are the available access modifiers:

  • The public keyword gives a type or type member public access, the most permissive access level. There are no restrictions on accessing public members.

  • The protected keyword gives a type or type member protected access, which means it's accessible from within the class in which it is declared, and from within any class derived from the class that declared this member. As discussed in the next chapter, a protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.

  • The internal keyword gives a type or type member internal access, which is accessible only within files in the same assembly. It is an error to reference a member with internal access outside the assembly within which it was defined. The C++ analog is friend .

  • The private keyword gives a type or type member private access, which 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. It is a compile-time error to reference a private member outside the class or the struct in which it is declared.

The following five accessibility levels can be specified using the access modifiers: public , protected , internal , internal protected , and private .


The C++ counterpart for the C# internal access modifier, which restricts members to access within the same assembly, is friend .

Note that if you don't specify an access modifier for a type or type member, the default access modifier is private . For example, you might have noticed that we explicitly declared the Addem method public in the Calculator class (see Listing 3.1). We did that so we could access Addem using an object of the Calculator class in the program's main class, ch03_01 . If we had another method, Subtractem , that we declared private in the Calculator class, Subtractem would be private to the Calculator class, which means that we can't access it using objects of that class. For example, this code won't compile:

 class ch03_01 {   static void Main()   {     Calculator obj = new Calculator();     System.Console.WriteLine("2 + 3 = {0}", obj.Addem(2, 3));  //This won't work!!   System.Console.WriteLine("3 - 2 = {0}", obj.Subtractem(3, 2));  } } class Calculator {   public long Addem(int value1, int value2)   {     return value1 + value2;   }  private long Subtractem(int value1, int value2)   {   return value1 - value2;   }  } 

Here's the error you'd see if you tried to compile this code:

 ch03_01.cs(7,49): error CS0122: 'Calculator.Subtractem(int, int)' is inaccessible due to its protection level 

We'll see the protected keyword, which you use primarily with class inheritance, in the next chapter, and discuss the internal keyword in more depth in Chapter 13, "Understanding C# Assemblies and Security," when we discuss assemblies.


C# doesn't support this kind of C++ class definition, where you can use one access modifier for multiple members:

 class Customer {   private:     string firstName;     string lastName;   public:     string GetName() {}     void SetName(string firstName, string lastName) {}     .     .     . } 

It's now time to get into some in-depth OOP as we create class members, including fields, methods, and properties.

Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: