Working with Structures, Enumerations, and Inheritance in C#


By Anand Narayanaswamy

In this article, we will examine the concept behind Structures, Enumerations, and Inheritance in C# with the help of relevant examples. In the end, you will also learn about Abstract Classes in C#.

Structures

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 preceding program, run the ILDASM tool and observe the exe file. You can view the compilation process of each and every line of the code.

Enumerations

Enumerations are a set of names for the corresponding numerical values. Normally, we use them to apply the code, as shown in Listing 2:

Listing 2

case 1:
  Console.WriteLine("OK");
  break;

case 2:
  Console.WriteLine("CANCEL");
  break;

Instead of 1 and 2 as in Listing 2, you can use meaningful constants like OK and CANCEL. This can be achieved through Enumerations.

Enumerations are defined using the enum keyword, as shown in Listing 3:

Listing 3

enum  Employees
{
  OK; //
  CANCEL;
}

The Employees enumeration defines two constants, OK and CANCEL. Each constant has its own numerical value. By default, the numbering system starts at 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 an integer by default, but you can change the type to byte, long, and so forth, as shown in Listing 5:

Listing 5

enum  Employees : byte
{
  OK = 50; 
  CANCEL = 100;
}

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.

Inheritance and Abstract Classes

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 the latter class is called the 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 the : operator. Consider the figure given below:



Click here for a larger image.

The relationship in the above figure can be expressed in C# as shown in Listing 6:

Listing 6

public class COMPUTER
{
  //code goes here
}

class COMPAQ:COMPUTER
{
  //code goes here
}

class DELL: COMPUTER
{
  //code goes here
}

C# doesn't support multiple inheritance. Hence, the piece of code shown in Listing 7 is illegal in C#:

Listing 7

Public class COMPUTER: COMPAQ, DELL
{
  //code goes here
}

C# introduces a new concept called Interfaces (to be discussed later), which is regarded as an alternative to Multiple Inheritance.

Sealed Classes

When a base class is declared with the sealed keyword (see Listing 8), that class cannot be extended. This is same as the final keyword in Java.

Listing 8

public sealed class Computer
{
  //code goes here
}

class COMPAQ:COMPUTER
{
  //Not allowed as base class is sealed
}

Abstract Classes

The Abstract class is a special type of class that should be declared with the abstract keyword. Moreover, it should contain one or more abstract methods, which should contain only method definitions. It won't have any method body (in the form of curly braces) as do the Instance and Static methods.

Normally, a base class is declared with the 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 because C# won't support 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

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.



About the Author

Anand Narayanaswamy

Anand Narayanaswamy (Microsoft MVP) is a freelance writer for Developer.com and Codeguru.com. He works as an independent consultant and runs NetAns Technologies (http://www.netans.com)which provides affordable web hosting services. He is the author of Community Server Quickly (http://www.packtpub.com/community-server/book). Anand also runs LearnXpress.com (http://www.learnXpress.com) and Dotnetalbum.com (http://www.dotnetalbum.com) and regularly contributes product and book reviews for various websites. He can be reached at ananddotnet@yahoo.co.in

Comments

  • what is the Different between Interfaces and Abstract Class

    Posted by ahm_anw83 on 07/13/2008 08:27pm

    Dear Sir, first of all i would like to thank you on your efforts,but i have missunderstand between the interface an abstract class. kindly if you can help me and support me to cover this porblem.

    Reply
  • Multiple Abstract Methods

    Posted by Legacy on 01/10/2004 12:00am

    Originally posted by: Manish

    If your abstract class has multiple abstract methods.
    
    I am deriving myClass from this abstract class. I want to implement few abstract methods of abstract class in myClass
    which is derived from abstract class.
    How will I do this in C#

    Reply
  • Error

    Posted by Legacy on 11/04/2002 12:00am

    Originally posted by: Sk

    I think the struct also is initiated with the same syntax as that for a class, ie with the 'new' operator...Pls cross check...

    Reply
  • Java has single class inheritance and multiple interface implementation

    Posted by Legacy on 10/10/2002 12:00am

    Originally posted by: Just a tought

    Java does not support multiple inheritance, neither Smalltalk. Except C++, I do not know any OO language that supports multiple inheritance.

    Also, interfaces are not new concept. Java is the first OO language to introduce interfaces. Microsoft COM has interfaces (which are basically C++ vtables).

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • QA teams don't have time to test everything yet they can't afford to ship buggy code. Learn how Coverity can help organizations shrink their testing cycles and reduce regression risk by focusing their manual and automated testing based on the impact of change.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds