Creating Classes, Structures, and Enumerations – C#

Almost all applications require creating one or more custom types, each with multiple properties and methods. In fact, proper object-oriented development design would place the bulk of the programming logic in these custom types.

For example, if you were creating an application that allowed you to edit a document, you would create a Document class. To update the document, you might call the Document.Update method. Depending on the purpose, you might create custom structures or enumerations rather than a custom class.

This page describes how to create custom classes, structures, and enumerations.

To create a custom class, use the Class keyword. Within the class, you can:

  • Declare properties using the Public or Private keywords. Properties are variables within your class.
  • Declare constructors using the New keyword in Visual Basic or by specifying a method using the class name in C#. Constructors are called to create a new instance of your class. You can provide multiple constructors to allow the calling method to provide different arguments when creating the class.
  • Declare methods within your class. Standard methods can be called after creating an instance of your class, and static methods can be called without creating an instance of your class. You can override standard methods, such as ToString.

The code sample below introduces you to the concept of creating a custom class. You will get more practice working with custom classes throughout this course.

The following code sample demonstrates how to create a new class named Person, create two instances of that class, access member properties, and then call a member method.

// Declare the class Person

class Person


    // Declare three public variables

    public string firstName;

    public string lastName;

    public DateTime dateOfBirth;

    // Provide a constructor with no arguments

    public Person()



    // Provide a constructor with three arguments

    public Person(string newFirstName, string newLastName, DateTime newDateOfBirth)


        // Assign the public variables using the arguments

        firstName = newFirstName;

        lastName = newLastName;

        dateOfBirth = newDateOfBirth;


    // Override the standard ToString() method

    public override string ToString()


        // Return the first and last names

        return firstName + " " + lastName;



static void Main()


    // Create an instance of Person

    Person david = new Person();

    // Define the person's name and birthdate

    david.firstName = "David";

    david.lastName = "Probst";

    david.dateOfBirth = new DateTime(1985, 5, 1);

    // Create an instance of person using the constructor that takes arguments

    Person chris = new Person("Chris", "Preston", new DateTime(1974, 1, 3));

    // Display the names of the two instances




Structures are a composite of other value types. The .NET Framework includes many structures, such as System.Drawing.Point. The Point class contains two value type properties: X and Y integers, as well as a number of methods.

You can work with structures similar to the way you work with other classes. However, because structures are value types, they can be used more efficiently in some circumstances.

To create a structure, use the Structure keyword in Visual Basic or the struct keyword in C#.

For example, the following code creates a type that represents the 360 degrees of a compass. In this case, adding one degree to 359 degrees should return the value to 0, and subtracting one degree from 0 degrees should set the value to 359. The + and – operators detect when calls attempt to pass these boundaries and set the correct value.

struct Compass


    // Private field only accessible from within the structure

    private int _val;

    // Public field used to grant external access to _val

    public int Value


        get { return _val; }



            if (value > 359)

                this.Value = value - 359 - 1;



                if (value < 0)

                    this.Value = 360 + value;


                    _val = value;




    // Allow output as a string

    public override string ToString()


        return Value.ToString();


    // Allow output as an integer

    public int ToInteger()


        return Value;


    // Allow inclusion in addition operations

    public static Compass operator +(Compass arg1, int degrees)


        arg1.Value += degrees;

        return arg1;


    // Allow inclusion in subtraction operations

    public static Compass operator -(Compass arg1, int degrees)


        arg1.Value -= degrees;

        return arg1;



Given that implementation of the Compass class, you could write the following console application to create an instance of the class and then perform addition and subtraction operations to represent turning:

// Create an instance of Compass

Compass direction = new Compass();

// Set the direction to West

direction.Value = 90;

Console.WriteLine("Direction: " + direction);

// Turn around, towards the left

direction.Value -= 180;

Console.WriteLine("Direction: " + direction);

// turn right

direction.Value += 90;

Console.WriteLine("Direction: " + direction);


Enumerations act like array constants. You should create an enumeration any time you want to be able to select one of several pre-defined values using friendly names within the development environment.

For instance, consider the following simple code sample. You could accomplish the same thing using an array of strings, but using enumerations is easier to develop with because Visual Studio can catch any typos before you compile your application.

enum Titles { Mr, Ms, Mrs, Dr };

Titles t = Titles.Dr; 

// Displays "Dr."

Console.WriteLine("{0}.", t);

Return to the .NET Framework Fundamentals Tutorials Table of Contents.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>