Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between 'Enum : Type' and simply 'Enum'

Tags:

c#

.net

enums

I am trying to understand a few things about Enums in general and how they can work with Chars specifically. Below is my example I am working from:

public enum AuditInteractionTypes
{
    Authorized = 'A',
    Created = 'C',
    Revised = 'R',
    Extracted = 'E',
    Deleted = 'D'
}

First, what's the difference between declaring them enum AuditInteractionTypes or enum AuditInteractionTypes : char

Second, I have seen the numerous post's about trying to use Enums with chars and how to "make" it work back and forth. Possible stupid question but why couldn't I simply go back and forth as a string.

So, for example, Authorized = "A".

I have am using Linq To SQL as my DAL if that matters though I am asking, I hope, a broader level question not specific to my environment.

like image 415
Refracted Paladin Avatar asked Dec 12 '12 14:12

Refracted Paladin


People also ask

What is the difference between enum and type?

Enums allow us to define or declare a collection of related values that can be numbers or strings as a set of named constants. Unlike some of the types available in TypeScript, enums are preprocessed and are not tested at compile time or runtime.

What is enum type?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

When should I use TypeScript enums?

In TypeScript, enums, or enumerated types, are data structures of constant length that hold a set of constant values. Each of these constant values is known as a member of the enum. Enums are useful when setting properties or values that can only be a certain number of possible values.

What is the difference between enum and enum in C#?

Enum is an abstract class, it is the common base class of all Java enumeration types while enum it's a category of classes that extend the Enum base class.


1 Answers

It dictates the underlying type that will be used for storage of the enumeration.

When you use enum without anything else, it uses an int as the underlying storage type.

When you use enum : <type>, it uses that type as the underlying storage type.

In your case, you're trying to make the underlying type of type char, but that's not valid, according to the C# reference:

The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.

If you want to store char values, then you have two options.

You could use an underlying type of ushort (it's an unsigned 16-bit integer like char), like so:

public enum AuditInteractionTypes : ushort
{
    Authorized = 'A',
    Created = 'C',
    Revised = 'R',
    Extracted = 'E',
    Deleted = 'D'
}

char has an implicit conversion to ushort so the above works. Also, you can easily compare the two.

If you want to use a string as the value then I'd recommend an enum-like class, like so:

public static class AuditInteractionTypes
{
    // You can make these static readonly if they are likely to change.
    public const string Authorized = "A";
    public const string Created = "C";
    public const string Revised = "R";
    public const string Extracted = "E";
    public const string Deleted = "D";
}

This class will then pretty much look the same as an enum and code the same way.

Note, the same trick can be done with any type, but generally those types should be completely immutable. string fills this guideline nicely, being completely immutable (as are most system value types, and other value types, if you've designed them correctly).

like image 198
casperOne Avatar answered Oct 05 '22 17:10

casperOne