Properties
Properties provide added functionality to the .NET Framework. Normally, we use accessor methods to modify and retrieve values in C++ and Visual Basic. If you have programmed using Visual Basic's ActiveX technology, this concept is not new to you. Visual Basic extensively uses accessor methods such as getXXX() and setXXX() to create user-defined properties.
A C# property consists of:
- Field declaration
- Accessor Methods (getter and setter methods)
Getter methods are used to retrieve the field's value and setter methods are used to modify the field's value. C# uses a special Value keyword to achieve this. Listing 10 declares a single field named zipcode and shows how to apply the field in a property.
Listing 10
using System;
class Propertiesexample
{
public string idValue;
public string IdValue
{
get
{
return idValue;
}
set
{
idValue = value;
}
}
public static void Main(string[] args)
{
Propertiesexample pe = new Propertiesexample();
pe.IdValue = "009878";
string p = pe.IdValue;
Console.WriteLine("The Value is {0}",p);
}
}
If you look at the MSIL generated by the code in Listing 10, you can view two methods, as shown in Listing 11:
Listing 11
Address::get_Zip() and Address::set_Zip().
We have reproduced the code for your reference. On careful scrutiny, you can see that the set_zip() method takes a string argument. This is because we have passed a string value while accessing the property. It's illegal to call these methods directly in a C# program.
In the preceding code, the Address.zipcode property is considered a read and write property because both getter and setter methods are defined. If you want to make the property read-only, omit the set block and to make it write only, omit the get block.
----------------------------------------------------------------------------------------------------------------------------------
Access Modifiers
Access modifiers determine the scope of visibility for variables and methods. There are four types of modifiers in C#: Public, Protected, Private, and Internal. For example, a variable declared as private in a super class cannot be called in a sub class. It should be declared as either public or protected. Let's examine each of these in detail.
- Public members are accessible from outside the class definition.
- Protected members are not visible outside the class and can be accessed only by derived or sub classes.
- Private member's scope is limited to the defined class. Derived classes or any other classes cannot access these members.
- Internal members are visible only within the current compilation unit.
-----------------------------------------------------------------------------------------------------------------------------------------
Enumerations
the data type for each constant in an enumeration is an integer by default, but you can change the type to byte, long, and so forth.
Listing 6 below illustrates how to apply the Employees enumeration in a C# program.
Listing 6
using System;
enum Employees
{
Instructors,
Assistants,
Counsellors
}
class Employeesenum
{
public static void Display(Employees e)
{
switch(e)
{
case Employees.Instructors:
Console.WriteLine("You are an Instructor");
break;
case Employees.Assistants:
Console.WriteLine("You are one of the Assistants");
break;
case Employees.Counsellors:
Console.WriteLine("You are a counsellor");
break;
default:break;
}
}
public static void Main(String[] args)
{
Employees emp;
emp = Employees.Counsellors;
Display(emp);
}
}
C# enumerations derive from System.Enum. Table 1 explains some of the important methods and properties of this class.
Table 1—List of Properties and methods of System.Enum class
Method Name |
Description |
GetUnderlyingType() |
Returns the data type used to represent the enumeration. |
Format() |
Returns the string associated with the enumeration. |
GetValues() |
Returns the members of the enumeration. |
Property Name |
Description |
IsDefined() |
Returns whether a given string name is a member of the current enumeration. |
----------------------------------------------------------------------------------------------------------------------------------------------
Interfaces
To rectify the drawback of multiple inheritance, the creators of C# have introduced a new concept called interfaces. Java programmers may be well aware of this concept. All interfaces should be declared with the keyword interface. You can implement any number of interfaces in a single derived class, but you should provide signatures to all method definitions of the corresponding interfaces.
Two or more interfaces can be combined into a single interface and implemented in a class,
You easily can determine whether a particular interface is implemented in a class by using is and as operators. The is operator enables you to check whether one type or class is compatible with another type or class; it returns a Boolean value. Listing 3 illustrates the usage of the is operator by revisiting Listing 2.
Listing 3
using System;
interface Interdemo
{
bool Show();
}
interface Interdemo1
{
bool Display();
}
class Interimp:Interdemo
{
public bool Show()
{
Console.WriteLine("Show() method Implemented");
return true;
}
public static void Main(string[] args)
{
Interimp inter = new Interimp();
inter.Show();
if(inter is Interdemo1)
{
Interdemo1 id = (Interdemo1)inter;
bool ok = id.Display();
Console.WriteLine("Method Implemented");
}
else
{
Console.WriteLine("Method not implemented");
}
}
}
Whereas the is operator returns a boolean value as shown in the preceding listing, the as operator returns null if there is any incompatibility between types. Listing 4 examines the usage of this operator. Here we have revisited Listing 3. Notice the change in code inside the Main () method.
Listing 4
using System;
interface Interdemo
{
bool Show();
}
interface Interdemo1
{
bool Display();
}
class Interimpas:Interdemo
{
public bool Show()
{
Console.WriteLine("Show() method Implemented");
return true;
}
public static void Main(string[] args)
{
Interimpas inter = new Interimpas();
inter.Show();
Interdemo1 id = inter as Interdemo1;
if(null!=id)
{
bool ok = id.Display();
Console.WriteLine("Method Implemented");
}
else
{
Console.WriteLine("Method not implemented");
}
}
}
Suppose you are declaring same method definitions in two different interfaces. The compiler will naturally show an error due to the ambiguity of the implemented method. Even if you use the "is" keyword, the compiler still will show warnings. To avoid this, you have to follow the syntax as shown in Listing 5:
Listing 5
Void <interface name>.<method name>
{
}
Listing 6 illustrates the application of the preceding concept in detail:
Listing 6
using System;
interface Interdemo
{
void Show();
}
interface Interdemo1
{
void Show();
}
class Interclash:Interdemo,Interdemo1
{
void Interdemo.Show()
{
Console.WriteLine("Show() method Implemented");
}
void Interdemo1.Show()
{
Console.WriteLine("Display() method Implemented");
}
public static void Main(string[] args)
{
Interclash inter = new Interclash();
inter.Interdemo.Show();
inter.Interdemo1.Show();
}
}
-------------------------------------------------------------------------------------------------------------------
About the Author
Anand Narayanaswamy works as a freelance Web/Software developer and technical writer. He runs and maintains learnxpress.com, and provides free technical support to users. His areas of interest include Web development, Software development using Visual Basic, and in the design and preparation of courseware, technical articles, and tutorials.