21

I came across some Enumerators that inherited uint. I couldn't figure out why anyone would do this.

Example:

Enum myEnum : uint
{
   ...
}

Any advantage or specific reason someone would do this? Why not just leave it as defaultint?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
  • Feb 2021: Microsoft's documentation (Nov 2019) states that Enums should be Int32. https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1028 – JimbobTheSailor Feb 16 '21 at 20:02

9 Answers9

31

if the enumeration values are greater than 2,147,483,647 and non-negative.

Tom Anderson
  • 10,807
  • 3
  • 46
  • 63
  • Why the down vote? I thought that was a valid reason to do this? – Tom Anderson Jan 05 '09 at 15:16
  • What about those values that only should not be negative? – Oliver Friedrich Jan 05 '09 at 15:19
  • 1
    Sorry, having a hard time figuring out what you mean, can you restate the question? – Tom Anderson Jan 05 '09 at 15:21
  • I didn't downvote it, but there's only one case I can imagine where this would apply... When you are using a flags enum that has to represent exactly 32 distinct booleans... Then it might make sense to use an uint to avoid switching to a long... But one more to 33, and you would need a long anyway. – Charles Bretana Jan 05 '09 at 15:22
  • 1
    I could agree with that in most circumstances, but when working with a 3rd party POS system, they had over 2200 flags stored in a single file in 32 bit chunks for faster indexing. Thus uint is used to store the values. – Tom Anderson Jan 05 '09 at 15:26
  • @Tom, in your scenario, did all 2200 flags potentially need to be represented in a single enum in application code logic? – Charles Bretana Jan 05 '09 at 15:59
  • yes, unfortunately, as there are a lot of "paired" flag combinations that don't exist in the same file space, its a huge mess to be honest. – Tom Anderson Jan 05 '09 at 16:05
  • I meant, that not only values greater than and non-negative would require a uint, but also values that should be non-negative. – Oliver Friedrich Jan 05 '09 at 16:08
13

One reason I can think of is that range checking is easier. Given that most enumerations start at 0, you'd only have to check the upper bound.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
9

I'd guess it's been done to allow it to be automatically cast to a uint (in C, enums were treated as ints by the type system). Quite possibly to allow bitwise ORing of flag values for the enum. see http://www.velocityreviews.com/forums/t286596-enums-in-c-and-c.html

Jesse Pepper
  • 3,225
  • 28
  • 48
9

It is convenient and absolutely necessary in some cases when working with COM Interop / P/Invoke from .NET. For example, you'll certainly find some at pinvoke.net defined this way. In some cases it just maps closer to the underlying API definition, but isn't necessary, but in some cases the underlying API might use some of those high bits for flags and it will be required to be UINT. Outside of Interop, you could use some very high bits as flags too just in plain C#/VB.NET and that might also require uint.

Scott Willeke
  • 8,884
  • 1
  • 40
  • 52
4

I'm converting some messaging code originally written in C into C#.

Some fields in the C code have a limited set of possible values defined as an associated group of #defines. In other words an enumeration.

When converting some of these enumerations, some values are too large for an integer e.g.

#define NONE 0x00000000

#define VAL1 0x00000001

...

#define VALX 0x80000000

Here, VALX won't be acceptable to a default enum in C#, so the enum has to be cast as a uint, which is also the type of the storing variable in the record structure.

1

I know of another reason, 'cos a colleague did it here.

To avoid the problem explained here (item 2), when generating a list of enums, he give it a unique value based on the string (I can't remember exactly how now, it can't be the hashcode 'cos that's not unique).

The only problem was that the numbers come out a bit big, so he made the enum a uint.

Community
  • 1
  • 1
Benjol
  • 63,995
  • 54
  • 186
  • 268
1

The example I'm talking about is in the code here: http://www.codeplex.com/FacebookNET

Take a look at the class FacebookResponseStatus.cs

I could email the author but I thought not to bother and just see what people think in the general community based on other experiences.

I am not going to be using this framework on CodePlex, but was reviewing it to see how others were implementing Facebook into .NET. I just thought it was interesting and wanted to know why the : uint for the Enums there.

1

looks like they are doing it purely for this one error, which means there are probably more errors not implemented in that class.

    /// <summary>
    /// There was an HTTP error in issuing the request.
    /// </summary>
    HttpError = 0xFFFF0000
Tom Anderson
  • 10,807
  • 3
  • 46
  • 63
0

This is so you can use bitwise math to store multiple items into one variable.

for example

[Flags]
    public enum SodaTypes: uint
    {
        None = 0b_0000_0000_0000_0000_0000_0000_0000_0000,
        PepsiNormal = 0b_0000_0000_0000_0000_0000_0000_0000_0001,
        DietPepsi = 0b_0000_0000_0000_0000_0000_0000_0000_0010,
        Coke = 0b_0000_0000_0000_0000_0000_0000_0000_0100,
        DietCoke = 0b_0000_0000_0000_0000_0000_0000_0000_1000,
        .
        .
        .
        .
        Fanta = 0b_1000_0000_0000_0000_0000_0000_0000_0000
    }

So once you can basically store multiple options like so:

SodasLiked = SodaTypes.Pepsi | SodaTypes.Fanta | SodaTypes.DietCoke;

and when you print this you will get:

Console.WriteLine(SodasLiked);

Output:

Pepsi, DietCoke,Fanta