Structures are basically value types. They are defined by
using the struct keyword. You can
access the variables inside a structure by creating an object of the structure.
The only difference is that you don’t have to use the syntax for creating an
object from a class, for structures. Listing 1 explains this concept clearly.
Listing 1
using
System;
enum
Employees:byte
{
ok
= 50,cancel = 100
}
struct
Emp
{
public
Employees EM;
public
string id;
}
class
Emptest
{
public
static void Main()
{
Emp
E;
E.EM
= Employees.cancel;
E.id
= "002";
Console.WriteLine(E.EM);
Console.WriteLine(E.id);
}
}
After executing the above program, run the ILDASM tool and observe the exe file. You can be able to view the compilation process of each and every line of the code.
Enumerations are a set of names for the corresponding
numerical values. Normally, we use to apply the code as shown below:
Listing 2
case
1:
Console.WriteLine(“OK”);
break;
case
2:
Console.WriteLine(“CANCEL”);
break;
Instead of 1 and 2 as in the above code, you can use
meaningful constants like ok and cancel. This can be achieved through
Enumerations.
Enumerations are defined using enum keyword as shown
in Listing 3:
Listing 3
enum
Employees
{
OK;
//
CANCEL;
}
The Employees enumeration defines two constants, OK and
CANCEL. Each constant is having its own numerical value. By default numbering
system starts from 0. However, you can change the order as shown in Listing 4:
Listing 4
enum
Employees
{
OK
= 50; //
CANCEL
= 100;
}
Also the data type for each constant in an enumeration is
integer by default. But you can change the type to byte, long etc as shown in
Listing 5:
Listing 5
enum
Employees : byte
{
OK
= 50;
CANCEL
= 100;
}
The Listing given 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.
|
Method
Name |
Description |
|
GetUnderlyingType() |
Returns the data type used to represent the enumeration. |
|
Returns the string associated with the enumeration. |
|
|
Returns the members of the enumeration. |
|
|
Property
Name |
Description |
|
IsDefined() |
Returns whether a given string name is a member of the current enumeration. |
The relationship between two or more classes is termed as Inheritance in an Object – Oriented Programming language. Normally there will be one class, from which the other classes may derive. The former class is called as Base class or super class and latter class is called as derived class. All variables and methods in the base class can be called in the derived classes, provided they are declared public or protected. In C#, classes are extended by means of : operator. Consider the figure given below

The relationship in the above figure can be expressed in C#
as follows:
Listing 6
public
class COMPUTER
{
//code
goes here
}
class
COMPAQ:COMPUTER
{
//code
goes here
}
class
DELL: COMPUTER
{
//code
goes here
}
C# doesn’t supports multiple inheritance. Hence, the
following piece of code is illegal in C#:
Listing 7
Public
class COMPUTER: COMPAQ, DELL
{
//code
goes here
}
C# introduces a new concept called Interfaces (will be discussed later), which is regarded as an alternative to Multiple Inheritance.
When a base class is declared with sealed keyword, then
that class cannot be extended. This is same as final keyword in Java.
Listing 8
public
sealed class Computer
{
//code
goes here
}
class
COMPAQ:COMPUTER
{
//Not
allowed as base class is sealed
}
Abstract class is a special type of class, which should be
declared with abstract keyword. Moreover,
it should contain one or more abstract methods, which should contain only method
definitions. It won’t be having any method body (in the form of curly braces)
like Instance and Static methods.
Normally, a base class is declared with abstract keyword
and the derived classes should extend the abstract class and implement relevant
methods. Keep in mind that only one abstract class can be extended at a time
since C# won’t supports multiple inheritance. Listing 9 illustrates this
concept clearly:
Listing 9
using
System;
abstract public class Absdemo
{
public abstract void Show();
}
class
Absimp:Absdemo
{
public override void Show()
{
Console.WriteLine("Abstract Method Implemented");
}
public static void Main(string[] args)
{
Absimp ai = new Absimp();
ai.Show();
}
}
About the Author