June 21, 2018
Hot Topics:

Working with Structures, Enumerations, and Inheritance in C#

  • October 10, 2002
  • By Anand Narayanaswamy
  • Send Email »
  • More Articles »

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 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";

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 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:

case 2:

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; //

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

class Employeesenum
  public static void Display(Employees e)

      case Employees.Instructors:
        Console.WriteLine("You are an Instructor");

      case Employees.Assistants:
        Console.WriteLine("You are one of the Assistants");

      case Employees.Counsellors:
        Console.WriteLine("You are a counsellor");



  public static void Main(String[] args) 
    Employees emp;
    emp = Employees.Counsellors;


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.

Page 1 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date