C# Classes instance members vs. enum

c

What is the difference between private variables in a clsClass type and using enumeration enum to instantiate the variable. This question arose when I was looking at some C# Class code for serial ports. example: private int SomeClassMember; and private enum TransmissionType{TEXT,HEX} ?

Where do the enum statements appear in a Class definition?

Best Answer

Adding to Eldritch Conundrum's answer (since he has not updated it):

In C#, enums can be declared in the same places as classes (which can be nested).

In many cases you might indeed be able to replace Enums
by simple constants or static classes with public constants, but there is more to Enums than meets the eye.

Intellisense (= auto completion in VS, MonoDevelop, etc...) provides support for enums, it does not do so for constants.

You should use enums to enforce strong typing on parameters, properties and return values that represent a set of values. The set of values should be closed, not subject to constant change.

You can extend enums:
Taken from: http://damieng.com/blog/2012/10/29/8-things-you-probably-didnt-know-about-csharp

public enum Duration { Day, Week, Month };

public static class DurationExtensions
{
    public static DateTime From(this Duration duration, DateTime dateTime) 
    {
        switch (duration)
        {
            case Duration.Day:
                return dateTime.AddDays(1);
            case Duration.Week:
                return dateTime.AddDays(7);
            case Duration.Month:
                return dateTime.AddMonths(1);
            default:
                throw new ArgumentOutOfRangeException("duration");
        }
    }
}

And of course Enums are usefull for Bitwise comparisons with the [Flags] attribute and in recent versions of .NET they have a .HasFlag method.

Not really an answer, just some extra info.