Home Classroom C# .NET C# .NET Part 4

C# .NET Part 4


During our April version we concentrated on the variables related to C#.NET. We discussed how variables are declared and initiated. You saw how easy it was to get into terms with the coding patterns and variable handling in C#.NET. Also discussed some examples where we used some variables in some sample codes. I assume that you must have done some thorough research on variables types in C# and how those are used in specific occasions. I believe that by now you should be enjoying programming with C#. NET and would be itching to go ahead.

 In this May version of C#.NET lesson, we would move one step forward. We would discuss about access modifiers, encapsulation and few other concepts. These are some of the most widely used concepts in programming especially in Object Oriented Programming. Hence this would be valid not only to C#.NET but also for other Object Oriented programming languages as well.

Access Modifiers

Access modifiers are an integral part of object-oriented programming. They support the concept of encapsulation, which promotes the idea of hiding functionality. Access modifiers allow you to define who does or doesn’t have access to certain features. Knowing about proper access modifiers and using them in a proper way would give you greater control over a programming language. There are instances where we can handle scenarios in a much safer and effective manner by using proper access modifiers.

In C#.NET there are few types of access modifiers, whose use would change based on the use of each modifier. Furthermore, visibility would change based on each modifier. Under mentioned are the access levels in C#.NET.

  • There are five levels of access allowed in .NET. The first access level provides access to everyone with no restrictions. This is called public, so any classes, methods, or properties defined as public are visible to all other code.

Ex: suppose there are 2 classes called Cat and Dog. A public method ‘run’ in Cat can be called in Dog. Hence all the features of ‘run’ would be there in Dog as well.

  • The exact opposite of this public access is private, which limits access to its own. Properties or methods defined as private are only accessible by code contained within the same class. Private elements may not be accessed by derived classes or other classes.

Ex: If a variable is declared inside a method, then that variable would only be valid inside that variable, it won’t be visible outside that method.

  • There are a few other access levels between the public and private spectrum. The protected access level says that properties and methods defined as protected are visible within its own class and any classes derived from it.

Ex: If there are 2 classes, where one class is a super class of the other, ‘protected’ keyword could be used for visibility. One class could be made the sup class of other. When that happens the variable could be made visible.

  • It says properties and methods defined as a friend or internal are accessible to all code by any class within its containing assembly. This level may be combined with the protected access level to define properties and methods that are accessible by all derived classes, as well as any classes within its assembly.

Here is a brief description about the access modifiers in C#.NET.

  • public: No access restrictions.
  • protected: Access limited to containing/derived classes.
  • internal: Access limited to containing type.
  • protected internal: Access limited to the current project.
  • private: Access limited to containing/derived classes and the current project.

Default Access

A default access level is used if no access modifier is specified in a member declaration. The following list defines the default access modifier for certain C# types:

  • enum: The default and only access modifier supported is public.
  • class: The default access for a class is private. It may be explicitly defined using any of the access modifiers.
  • interface:The default and only access modifier supported is public.
  • struct:The default access is private with public and internal supported as well.

The default access may suffice for a given situation, but you should specify the access modifier you want to use to ensure proper application behavior.

In this example let’s see how access modifiers are used to hide the visibility of variables. The purpose and use of each line is explained with each code segment.

  • All ‘enums’ are based upon an underlying integral type, which if not specified, defaults to int. The underlying type of the enum must be an integral type that is one of the following: byte, sbyte, short, ushort, int, uint, long, or ulong.
  • ‘class’ is a blue print of an object, all the code segments which associates with an object is included in a class. And based on that object s could be made. If there a class called ‘Person’

Ex: Person p1 = new Person (), Person p2 = new Person ();

p1 and p2 are object names.

An Interface is a reference type and it contains only abstract members. Interface's members can be Events, Methods, Properties and Indexers. But the interface contains only declaration for its members. Any implementation must be placed in class that realizes them. The interface can't contain constants, data fields, constructors, destructors and static members. All the member declarations inside interface are implicitly public.

A struct allows you to create new value-type objects that are similar to the built-in types (int, float, bool, etc.). Structs are custom value types that store the values in each field together. They do not store referenced data, such as the character array in a string.

Figure 1: Access Modifiers Visibility

In this example all the four variables we declared are applicable to the entire class ‘First’. Class ‘NestedClass’ is an inner class of the class ‘First’. Hence those variables are accessible from the class from that inner class.

P.N: In C#.NET when you press the ‘.’ a list appears. If a particular variable, method isn’t visible from certain location in the class, that variable or method wouldn’t be in that list. During the development stage this would give the developer an indication about the visibility of that particular variable or method.

Figure 2: More examples on Access Modifiers

Segment A shows, making an object of the ‘First’ class, and trying to display the variables which are in that class. As you can see ‘var_one’ and ‘var_two’ would display errors as they are not visible through the object. Because making them ‘private’ would mean that these are only visible within the class, not from outside. There isn’t such problem with the other two variables.

In the next section class ‘DerivedClass’ had inherited from the First class. In this case the scenario changes a bit. But as usual ‘var_one’ with private access modifier returns an error message, because it is not accessible from another class. But variables with ‘protected’ access modifier can be accessed through this derived class. This is a very important fact when it comes to programming.

In the ’C’ section it is very similar to the ‘B’ section, the only difference is that this is a private class and ‘Derived’ class is given as the argument to the method. But because of the ‘Derived’ class, the result is identical to the ‘B’ section.

The last section is similar to the ‘C’ section, but the different is that the argument here is the ‘First’ class, hence the output is similar to section’ A’. This is similar to creating an instance of ‘First’ class. Hence the results too are similar ‘A’.

As you saw in the previous examples the access of the variables and methods could differ based on the modifier we use during each occasion. Try to understand more about these access modifiers and try to get used to the usage of each of the modifiers.

This would be the end of our article for the month of May. In our next article we would concentrate more on object oriented concepts, which would be very advantageous in programming with C#.NET.



Lakpriya Kottahachchi is an Undergraduate at the Faculty of Information Technology, University of Moratuwa. He enjoys programming with Java and C#.


Leave a Reply