Click here to Skip to main content
15,888,610 members
Articles / Programming Languages / C#
Article

Enums and Structs in C#

Rate me:
Please Sign up or sign in to vote.
4.84/5 (67 votes)
13 Oct 2001CPL4 min read 476.1K   4.1K   107   40
Two oft-overlooked value types that C# offers and where they can be used

Introduction

Just about everything is a heap object when you are using C#. Only elementary native types like int are treated as value types. But there are two value types in C# that are pretty much more useful that first glances would tell you. They are the enum and struct types. Very few tutorials even cover these topics, but they have their own uses. And both of them are a lot more efficient than classes and you can use them in place of classes when they meet your requirements to improve performance.

Enums

Enums are basically a set of named constants. They are declared in C# using the enum keyword. Every enum type automatically derives from System.Enum and thus we can use

C#
System.Enum
methods on our Enums. Enums are value types and are created on the stack and not on the heap. You don't have to use new to create an enum type. Declaring an enum is a little like setting the members of an array as shown below.

C#
enum Rating {Poor, Average, Okay, Good, Excellent}

You can pass enums to member functions just as if they were normal objects. And you can perform arithmetic on enums too. For example we can write two functions, one to increment our  enum and the other to decrement our enum.

C#
Rating IncrementRating(Rating r)
{
    if(r == Rating.Excellent)
        return r;
    else
        return r+1;
}
Rating DecrementRating(Rating r)
{
    if(r == Rating.Poor)
        return r;
    else
        return r-1;
}

Both functions take a Rating object as argument and return back a Rating object. Now we can simply call these functions from elsewhere.

C#
for (Rating r1 = Rating.Poor; 
    r1 < Rating.Excellent ; 
    r1 = IncrementRating(r1))
{           
    Console.WriteLine(r1);
}

Console.WriteLine();

for (Rating r2 = Rating.Excellent; 
    r2 > Rating.Poor; 
    r2 = DecrementRating(r2))
{
    Console.WriteLine(r2);          
}

And here is a sample code snippet showing how you can call

C#
System.Enum
methods on our Enum object. We call the
C#
GetNames
method which retrieves an array of the names of the constants in the enumeration.

C#
foreach(string s in Rating.GetNames(typeof(Rating)))
    Console.WriteLine(s);

Where to use enums

Quite often we have situations where a class method takes as an argument a custom option. Let's say we have some kind of file access class and there is a file open method that has a parameter that might be one of read-mode, write-mode, read-write-mode, create-mode and append-mode. Now you might think of adding five static member fields to your class for these modes. Wrong approach! Declare and use an enumeration which is a whole lot more efficient and is better programming practice in my opinion.

Structs

In C++ a struct is just about the same as a class for all purposes except in the default access modifier for methods. In C# a

C#
struct
are a pale puny version of a class. I am not sure why this was done so, but perhaps they decided to have a clear distinction between structs and classes. Here are some of the drastic areas where classes and structs differ in functionality.

  • structs are stack objects and however much you try you cannot create them on the heap
  • structs cannot inherit from other structs though they can derive from interfaces
  • You cannot declare a default constructor for a struct, your constructors must have parameters
  • The constructor is called only if you create your struct using new, if you simply declare the struct just as in  declaring a native type like int, you must explicitly set each member's value before you can use the
    C#
    struct
    
C#
struct Student : IGrade
{   
    public int maths;
    public int english;
    public int csharp;

    //public member function
    public int GetTot()
    {
        return maths+english+csharp;
    }

    //We have a constructor that takes an int as argument
    public Student(int y)
    {
        maths = english = csharp = y;
    }

    //This method is implemented because we derive
    //from the IGrade interface
    public string GetGrade()
    {
        if(GetTot() > 240 )
            return "Brilliant";
        if(GetTot() > 140 )
            return "Passed";
        return "Failed";
    }
}

interface IGrade
{
    string GetGrade();
}

Well, now let's take a look at how we can use our struct.

C#
Student s1 = new Student();
Console.WriteLine(s1.GetTot());
Console.WriteLine(s1.GetGrade());

//Output
0
Failed

Here the default constructor gets called. This is automatically implemented for us and we cannot have our own default parameter-less constructor. The default parameter-less constructor simply initializes all values to their zero-equivalents. This is why we get a 0 as the total.

C#
Student s2;
s2.maths = s2.english = s2.csharp = 50;
Console.WriteLine(s2.GetTot());
Console.WriteLine(s2.GetGrade());

//Output
150
Passed

Because we haven't used new, the constructor does not get called. Of all the silly features this one must win the annual contest by a long way. I see no sane reason why this must be so. Anyway you have to initialize all the member fields. If you comment out the line that does the initialization you will get a compiler error :- Use of unassigned local variable 's2'

C#
Student s3 = new Student(90);
Console.WriteLine(s3.GetTot());
Console.WriteLine(s3.GetGrade());

//Output
270
Brilliant

This time we use our custom constructor that takes an int as argument.

When to use structs

Because structs are value types they would be easier to handle and more efficient that classes. When you find that you are using a class mostly for storing a set of values, you must replace those classes with structs. When you declare arrays of structs because they are created on the heap, efficiency again improves. Because if they were classes each class object would need to have memory allocated on the heap and their references would be stored. In fact lots of classes within the .NET framework are actually structs. For example

C#
System.Drawing.Point
is actually a struct and not a
C#
class
.

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions

 
GeneralRe: Best practice of using enums and a data source Pin
James T. Johnson17-Oct-02 14:40
James T. Johnson17-Oct-02 14:40 
GeneralRe: Best practice of using enums and a data source Pin
Nish Nishant17-Oct-02 15:27
sitebuilderNish Nishant17-Oct-02 15:27 
GeneralRe: Best practice of using enums and a data source Pin
James T. Johnson17-Oct-02 15:54
James T. Johnson17-Oct-02 15:54 
GeneralRe: Best practice of using enums and a data source Pin
Paul Watson17-Oct-02 20:19
sitebuilderPaul Watson17-Oct-02 20:19 
GeneralRe: Best practice of using enums and a data source Pin
Nish Nishant17-Oct-02 20:27
sitebuilderNish Nishant17-Oct-02 20:27 
GeneralRe: Best practice of using enums and a data source Pin
Paul Watson17-Oct-02 20:34
sitebuilderPaul Watson17-Oct-02 20:34 
GeneralRe: Best practice of using enums and a data source Pin
Nish Nishant17-Oct-02 20:55
sitebuilderNish Nishant17-Oct-02 20:55 
GeneralRe: Best practice of using enums and a data source Pin
bortlip24-Mar-12 3:03
bortlip24-Mar-12 3:03 
I know this message is from about a decade ago, but for anyone reading this now with this problem, here's

how I handle it:

Use code generation. A simple way to do so is to use XSD.exe from Microsoft, although it has some pain points. Keep an XSD file that defines all of the enums. For example:

XML
<xs:simpleType name="Type">
    <xs:restriction base="xs:string">
        <xs:enumeration value="Article" />
        <xs:enumeration value="Image" />
        <xs:enumeration value="ABC" />
        <xs:enumeration value="XYZ" />
    </xs:restriction>
</xs:simpleType>


You then have an XSD definition for each of your entity classes. If a particular entity class has the property Type, then the XSD for that class contains an
<xs:Element name="PropertyNameYouWantForClassDefinintion" type="Type" />
definition.

XSD.exe will then take your XSD file as input and generate a .cs file with classes for each xs:complexType and enums for each xs:simpleType defined with the enum constraints as shown above.

So, for the above xs:simpleType, the following enum code is generated in the resulting .cs file:

VB
public enum Type {
    Article,
    Image,
    ABC,
    XYZ
}


A xs:complexType that references a xs:simpletype will have a property generated with the property's type being the type generated for the xs:simpleType. In the above case, it would be of type "Type" and have a value of one of the listed enum values.

The generated class are partial classes, so any custom logic (ie, all funtions/method or custom get/sets) are put in a separate .cs file that is not generated. That way, when an enum gets updated (or any change happens to your XSD), you can regenerate the generated .cs files and your custom code isn't wiped out. You should never hand edit a generated file because the next time you regenerate, you'll lose any hand edits you have made.

The generated classes are also decorated with the proper attributes so that XML serialization is a simple function call or two. So you can with a line or two of code read and write your live data to XML that is compliant with your defined XSD. The XML that gets generated will use the text values you defined for the enums, so no need to translate between text and numbers (IE, no Article = 1, though you could in code if you wanted to for some reason).

It's a system that take a little bit of start up effort - defining all your XSD(s). But it saves immense time and effort in the long run.

I use this system at work where we also then expose these XSDs to external groups as web service end points. We can just provide them an XSD that references our class/enum XSD for the types they pass and use serialization to populate objects at run time from their xml messages so our code that processes the messages that come in only ever works with objects and doesn't care about the XML. We then have a generic XML Request processing service that will take all XML requests (that it can validate), instantiate and populate the appropriate objects and let them process the requests. Adding a new request types, say sending a new type of data, means only having to define the new XSD, add it to the list of valid XSDs that the request processor accepts, and write the code that takes the request information as an object and returns a result that gets XML serialized and sent back. Works great!!
GeneralWhere to locate an enum Pin
Paul Watson17-Oct-02 4:42
sitebuilderPaul Watson17-Oct-02 4:42 
GeneralRe: Where to locate an enum Pin
Nish Nishant17-Oct-02 5:45
sitebuilderNish Nishant17-Oct-02 5:45 
GeneralRe: Where to locate an enum Pin
Paul Watson17-Oct-02 6:21
sitebuilderPaul Watson17-Oct-02 6:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.