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?
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?
if the enumeration values are greater than 2,147,483,647 and non-negative.
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.
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
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.
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.
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.
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.
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
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