I am trying to map a ulong to a long (and vice-versa), and a uint to a int (and vice-versa), as shown below - in order to save the values in a MS-SQL-database with signed types integer and biginteger only.
I do this because I have to check (in the database) whether a number (uint, ulong) is within which range in a bunch of uint/ulong ranges (IPs - v4 & v6; actually the ulong is in reality a uint128 composed of two ulongs).
Is there a more efficient way to accomplish this than the code I have here:
public static ulong SignedLongToUnsignedLong(long signedLongValue)
{
ulong backConverted = 0;
// map ulong to long [ 9223372036854775808 = abs(long.MinValue) ]
if (signedLongValue < 0)
{
// Cannot take abs from MinValue
backConverted = (ulong)System.Math.Abs(signedLongValue - 1);
backConverted = 9223372036854775808 - backConverted - 1;
}
else
{
backConverted = (ulong)signedLongValue;
backConverted += 9223372036854775808;
}
return backConverted;
}
public static long UnsignedLongToSignedLong(ulong unsignedLongValue)
{
// map ulong to long [ 9223372036854775808 = abs(long.MinValue) ]
return (long) (unsignedLongValue - 9223372036854775808);
}
public static int UnsignedIntToSignedInt(uint unsignedIntValue)
{
// map uint to int [ 2147483648 = abs(long.MinValue) ]
return (int)(unsignedIntValue - 2147483648);
}
public static uint SignedIntToUnsignedInt(int signedIntValue)
{
uint backConverted = 0;
// map ulong to long [ 2147483648 = abs(long.MinValue) ]
if (signedIntValue < 0)
{
// Cannot take abs from MinValue
backConverted = (uint)System.Math.Abs(signedIntValue - 1);
backConverted = 2147483648 - backConverted - 1;
}
else
{
backConverted = (uint)signedIntValue;
backConverted += 2147483648;
}
return backConverted;
}
public static void TestLong()
{
long min_long = -9223372036854775808;
long max_long = 9223372036854775807;
ulong min_ulong = ulong.MinValue; // 0
ulong max_ulong = ulong.MaxValue; // 18446744073709551615 = (2^64)-1
long dbValueMin = UnsignedLongToSignedLong(min_ulong);
long dbValueMax = UnsignedLongToSignedLong(max_ulong);
ulong valueFromDbMin = SignedLongToUnsignedLong(dbValueMin);
ulong valueFromDbMax = SignedLongToUnsignedLong(dbValueMax);
System.Console.WriteLine(dbValueMin);
System.Console.WriteLine(dbValueMax);
System.Console.WriteLine(valueFromDbMin);
System.Console.WriteLine(valueFromDbMax);
}
public static void TestInt()
{
int min_int = -2147483648; // int.MinValue
int max_int = 2147483647; // int.MaxValue
uint min_uint= uint.MinValue; // 0
uint max_uint = uint.MaxValue; // 4294967295 = (2^32)-1
int dbValueMin = UnsignedIntToSignedInt(min_uint);
int dbValueMax = UnsignedIntToSignedInt(max_uint);
uint valueFromDbMin = SignedIntToUnsignedInt(dbValueMin);
uint valueFromDbMax = SignedIntToUnsignedInt(dbValueMax);
System.Console.WriteLine(dbValueMin);
System.Console.WriteLine(dbValueMax);
System.Console.WriteLine(valueFromDbMin);
System.Console.WriteLine(valueFromDbMax);
}
Option 1: order-preserving map
It sounds like you're asking for a map which preserves order, meaning that, for example, if x and y are ulongs and x < y, then MapUlongToLong(x) < MapUlongToLong(y).
Here's how to do that:
To map from ulong to long, cast and add long.MinValue. To map from long back to ulong, subtract long.MinValue and cast. In either case, use an unchecked context so that overflow conditions are ignored.
public static long MapUlongToLong(ulong ulongValue)
{
return unchecked((long)ulongValue + long.MinValue);
}
public static ulong MapLongToUlong(long longValue)
{
return unchecked((ulong)(longValue - long.MinValue));
}
The logic for uint and int is exactly analogous.
(Option 1 is the original answer I wrote in 2016. I added option 2, and the comparison of the two, in 2021.)
Option 2: non-order-preserving map
I don't think this is what you're asking for, but it's even easier to do the conversion if you don't care about preserving order.
These functions work the same way as the above functions, except that we don't bother to add or subtract long.MinValue.
public static long MapUlongToLong(ulong ulongValue)
{
return unchecked((long)ulongValue);
}
public static ulong MapLongToUlong(long longValue)
{
return unchecked((ulong)longValue);
}
Which option is better?
Option 1 preserves order and option 2 doesn't, so if you need to preserve order, use option 1.
How long do the functions in option 1 take to execute? Well, those functions will probably be inlined and optimized by the JIT compiler, and they're ultimately asking the CPU to do something very, very simple. I'm guessing that each function call will take less than 1 nanosecond.
One of the comments describes this less-than-a-nanosecond execution time as being "relatively slow." If a nanosecond is too slow for you, you may want to use option 2.
The functions in option 2 will also probably be inlined and optimized by the JIT compiler, and it turns out that as far as the CPU is concerned, those functions do literally nothing. Therefore, no machine code will be generated for those functions, and so each function call will take no time at all—in other words, 0 nanoseconds.
Aron's answer does the same thing as option 2, and I'm guessing that it will run equally fast, too.
Althought Tanner Swett is correct. A much nicer and dirty solution is to tell .net to map access for a ulong to the same memory address as a long. This will give you instantaneous conversion speed.
void Main()
{
var foo = new Foo { Long = -1 };
Console.WriteLine(foo.ULong);
}
// Define other methods and classes here
[StructLayout(LayoutKind.Explicit)]
public class Foo
{
[FieldOffset(0)]
private ulong _ulong;
[FieldOffset(0)]
private long _long;
public long Long
{
get { return _long; }
set { _long = value; }
}
public ulong ULong
{
get { return _ulong; }
set { _ulong = value; }
}
}
By setting your entity framework POCO to use the attributes shown, you can control the memory addresses that the fields are mapped to.
Therefore, no conversion ever occurs.
This code is 100% faster than Tanner Swett's.
Necromancing.
Generic answer based on the answer of Tanner Swett:
private static class Number<T>
{
private static object GetConstValue(System.Type t, string propertyName)
{
System.Reflection.FieldInfo pi = t.GetField(propertyName, System.Reflection.BindingFlags.Static
| System.Reflection.BindingFlags.Public
| System.Reflection.BindingFlags.NonPublic
);
return pi.GetValue(null);
}
private static T GetMinValue<T>()
{
return (T)GetConstValue(typeof(T), "MinValue");
}
private static T GetMaxValue<T>()
{
return (T)GetConstValue(typeof(T), "MaxValue");
}
private static System.Func<T, T, T> CompileAdd<T>()
{
// Declare the parameters
System.Linq.Expressions.ParameterExpression paramA =
System.Linq.Expressions.Expression.Parameter(typeof(T), "a");
System.Linq.Expressions.ParameterExpression paramB =
System.Linq.Expressions.Expression.Parameter(typeof(T), "b");
// Add the parameters
System.Linq.Expressions.BinaryExpression body =
System.Linq.Expressions.Expression.Add(paramA, paramB);
// Compile it
System.Func<T, T, T> add =
System.Linq.Expressions.Expression.Lambda<System.Func<T, T, T>>
(body, paramA, paramB).Compile();
return add;
}
private static System.Func<T, T, T> CompileSubtract<T>()
{
// Declare the parameters
System.Linq.Expressions.ParameterExpression paramA =
System.Linq.Expressions.Expression.Parameter(typeof(T), "a");
System.Linq.Expressions.ParameterExpression paramB =
System.Linq.Expressions.Expression.Parameter(typeof(T), "b");
// Subtract the parameters
System.Linq.Expressions.BinaryExpression body =
System.Linq.Expressions.Expression.Subtract(paramA, paramB);
// Compile it
System.Func<T, T, T> subtract =
System.Linq.Expressions.Expression.Lambda<System.Func<T, T, T>>
(body, paramA, paramB).Compile();
return subtract;
}
public static T MinValue = GetMinValue<T>();
public static T MaxValue = GetMaxValue<T>();
public static System.Func<T, T, T> Add = CompileAdd<T>();
public static System.Func<T, T, T> Subtract = CompileSubtract<T>();
}
public static TSigned MapUnsignedToSigned<TUnsigned, TSigned>(TUnsigned ulongValue)
{
TSigned signed = default(TSigned);
unchecked
{
signed = Number<TSigned>.Add((TSigned)(dynamic)ulongValue, Number<TSigned>.MinValue);
}
return signed;
}
public static TUnsigned MapSignedToUnsigned<TSigned, TUnsigned>(TSigned longValue)
{
TUnsigned unsigned = default(TUnsigned);
unchecked
{
unsigned = (TUnsigned)(dynamic) Number<TSigned>
.Subtract(longValue, Number<TSigned>.MinValue);
}
return unsigned;
}
equivalent:
// return MapUnsignedToSigned<ulong, long>(ulongValue);
private static long MapULongToLong(ulong ulongValue)
{
return unchecked((long)ulongValue + long.MinValue);
}
// return MapSignedToUnsigned<long, ulong>(longValue);
private static ulong MapLongToUlong(long longValue)
{
return unchecked((ulong)(longValue - long.MinValue));
}
Related
Have this code:
string abc = "123456";
To convert to int should I use convert:
int abcInt = Convert.ToInt32(abc);
The problem is that if not a number I have an exception see returning zero so my final code will look like:
try{ int abcInt = Convert.ToInt32(abc); }catch(Exception e){ int abcInt = 0; }
So you see that I decided to create a book that made me an object returning zero numeric without exception if it failed, so could keep most flexible programming without much junk code:
int abcInt = Libs.str.safeInt(abc);
The code is:
public int safeInt(object ob)
{
if ((ob == null) || (String.IsNullOrEmpty(ob.ToString())))
return 0;
try
{
return Convert.ToInt32(
System.Text.RegularExpressions.Regex.Replace(ob.ToString(), #"#[^Ee0-9\.\,]+#i", "").
ToString(CultureInfo.InvariantCulture.NumberFormat)
);
}
catch (FormatException e)
{
return 0;
}
}
But I want to go one step further and do something like this:
int abcInt = (safeInt)abc;
how to do?
Can not convert type 'string' to 'Libs.safeInt.safeInt'
You should just use Int32.TryParse:
int abcInt;
if(!Int32.TryParse(abc, out abcInt)) {
abcInt = 0;
}
// abcInt has been parsed to an int, or defaulted to zero
Note that this can be shortened to
int abcInt;
Int32.TryParse(abc, out abcInt);
if all that you want is the default value to be zero because:
When this method returns, contains the 32-bit signed integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.
I actually recommend against writing it this way because now you can't distinguish between abc = "0" and abc = "garbage"; both with exhibit exactly the same behavior with the above two lines of code. With the initial version above (i.e., the if, you can distinguish the two cases if you need to; silently ignoring errors is generally a bad idea).
That said, if you absolutely are dying to know how to implement an explicit cast operator, you proceed like this:
class SafeInt32 {
private readonly int value;
public int Value { get { return this.value; } }
private readonly string source;
public string Source { get { return this.source; } }
private readonly bool successful;
public bool Successful { get { return this.successful; } }
public SafeInt32(string source) {
this.source = source;
this.successful = Int32.TryParse(source, out this.value);
}
public static explicit operator SafeInt32(string source) {
return new SafeInt32(source);
}
public static implicit operator int(SafeInt32 safeInt32) {
return safeInt32.Value;
}
}
Usage:
int abcInt = (SafeInt32)"123456";
Note that we had to define an explicit cast operator to cast a string to a SafeInt32, and an implicit cast operator to cast a SafeInt32 to an int to achieve your desired syntax. The latter is necessary so that the compiler can silently convert the result of (SafeInt32)"123456" to an int.
Again, I recommend against this; use Int32.TryParse.
You can leverage implicit and explicit operators to do what you want, yes. You can also use int.TryParse to avoid using exceptions for control flow.
public struct SafeInt
{
public int Value { get; private set; }
public static implicit operator int(SafeInt safeInt)
{
return safeInt.Value;
}
public static explicit operator SafeInt(string obj)
{
return new SafeInt() { Value = SafeParse(obj) };
}
public static int SafeParse(object value)
{
int output;
int.TryParse((value ?? "0").ToString(), out output);
return output;
}
}
I mean, you should use int.TryParse, but if you're dead-set on the cast syntax:
public class SafeInt
{
private int _value;
private SafeInt() {}
public static explicit operator SafeInt(string str)
{
int x;
int.TryParse(str, out x);
SafeInt si = new SafeInt();
si._value = x;
return si;
}
public static implicit operator int(SafeInt x)
{
return x._value;
}
public override string ToString()
{
return _value.ToString();
}
}
You can then use it like this:
int x = (SafeInt)"234234";
First, let me just go on record saying that you may not want to do this.
Silently ignoring problems like this can cause other types of problems, such as a customer asking "Why is this total over here always wrong?".
Having said that, let's see how you can do what you want before I give you a better option:
void Main()
{
int a = (SafeInt)"123";
a.Dump();
int b = (SafeInt)"xyz";
b.Dump();
}
public struct SafeInt
{
private readonly int _Value;
public SafeInt(int value)
{
_Value = value;
}
public SafeInt(int? value)
{
_Value = value ?? 0;
}
public int Value
{
get
{
return _Value;
}
}
public static implicit operator int(SafeInt s)
{
return s.Value;
}
public static implicit operator SafeInt(string s)
{
try
{
return new SafeInt(Convert.ToInt32(s));
}
catch (FormatException)
{
return new SafeInt();
}
}
}
This will print out:
123
0
Now, my advice is to stay away from this. Instead, use this:
void Main()
{
TryParse("123").Dump();
TryParse("xyz").Dump();
}
public static int TryParse(string s, int errorValue = 0)
{
int result;
if (int.TryParse(s, out result))
return result;
return errorValue;
}
Note that if you always want 0 as the value to return upon an error, there's even a much simpler way built into the system, this:
int value;
int.TryParse("123", out value);
Here we disregard the Boolean result from TryParse, because if TryParse fails, it'll set the parameter to 0.
I'd recommend that you do not do this. I find explicit and implicit conversions to be hard to discover, read, and use, compared to simpler static methods and/or constructors. Also, are you aware of the int.TryParse method? That might be a better solution for you:
public static int SafeInt(object value)
{
int i;
int.TryParse(value.ToString(), out i);
return i;
}
Or, more directly to answer your question, you can use explicit and implicit conversions on a SafeInt class to do this:
public class SafeInt
{
public int Value { get; set; }
public static implicit operator int(SafeInt si)
{
return si.Value;
}
public static explicit operator SafeInt(String str)
{
return new SafeInt { Value = Libs.str.safeInt(str) };
}
}
Use like:
int i = (SafeInt)"123";
I need to super fast store and retrieve values by two integer keys.
So I have input values uint Id1, uint Id2 and need to get uint Count.
Also I know max value of Id1 and Id2 (it is about 5 000 000).
My current implementation takes about 70% of application work time and it might be a few days.
It just use standard .net dictionaries and of course can be improved. But I guess it is a very useful operation in computer science and no doubt more efficient algorithms exists.
Here is my implementation
void Main()
{
var rep = new Repository();
var sw = new Stopwatch();
sw.Start();
for (uint i = 0; i < 10000; i++)
{
for (uint j = 0; j < 1000; j++)
{
rep.Add(new DomainEntity(){Id1 = i, Id2 = j, Count = 1});
}
}
for (uint i = 0; i < 10000; i++)
{
for (uint j = 0; j < 1000; j++)
{
rep.GetDomainEntityByIds(i,j);
}
}
sw.Stop();
Console.WriteLine ("Elapsed:{0}", sw.Elapsed);
}
public class Repository
{
private readonly Dictionary<Tuple<UInt32, UInt32>, UInt32> _dictStore;
public Repository()
{
_dictStore = new Dictionary<Tuple<uint, uint>, uint>();
}
public uint Add(DomainEntity item)
{
var entry = MapToTableEntry(item);
_dictStore.Add(entry.Key,entry.Value);
return 0;
}
public void Update(DomainEntity item)
{
var entry = MapToTableEntry(item);
_dictStore[entry.Key] = entry.Value;
}
public IEnumerable<DomainEntity> GetAllItems()
{
return _dictStore.Select(MapToDomainEntity);
}
public DomainEntity GetDomainEntityByIds(uint articleId1, uint articleId2)
{
var tuple = new Tuple<uint, uint>(articleId1, articleId2);
if (_dictStore.ContainsKey(tuple))
{
return MapToDomainEntity(new KeyValuePair<Tuple<uint, uint>, uint>(tuple, _dictStore[tuple]));
}
return null;
}
private KeyValuePair<Tuple<uint, uint>, uint> MapToTableEntry(DomainEntity item)
{
return new KeyValuePair<Tuple<uint, uint>, uint>(new Tuple<uint, uint>(item.Id1,item.Id2), item.Count);
}
private DomainEntity MapToDomainEntity(KeyValuePair<Tuple<uint, uint>, uint> entry)
{
return new DomainEntity
{
Id1 = entry.Key.Item1,
Id2 = entry.Key.Item2,
Count = entry.Value,
};
}
}
public class DomainEntity
{
public uint Id1 { get; set; }
public uint Id2 { get; set; }
public uint Count { get; set; }
}
One minor(?) improvement, you can use TryGetValue to avoid to lookup the dictionary twice:
public DomainEntity GetDomainEntityByIds(uint articleId1, uint articleId2)
{
var tuple = new Tuple<uint, uint>(articleId1, articleId2);
uint value;
if (_dictStore.TryGetValue(tuple, out value))
{
return MapToDomainEntity(new KeyValuePair<Tuple<uint, uint>, uint>(tuple, value));
}
return null;
}
What you want to do is create an efficient dictionary using an efficient key & hash. Since the dictionary always uses a 32 bit value and you have around 45 bits of data, you can't create a unique hash, but you should do your best.
Always use TryGetValue() rather than a double lookup.
When using dictionaries with value type keys, use a custom IEqualityComparer passed as the argument to the dictionary constructor.
Use a custom hash code to try to squeeze the maximum amount of information from the subkeys into the 32 bit hash.
Example:
public class Storage
{
private Dictionary<Key, DomainObject> dict;
public Storage()
{
dict = new Dictionary<Key, DomainObject>(Key.Comparer.Instance)
}
public DomainObject Get(uint a, uint b)
{
DomainObject obj;
dict.TryGetValue(new Key(a,b), out obj);
return obj;
}
internal struct Key
{
internal readonly uint a;
internal readonly uint b;
public Key(uint a, uint b)
{
this.a = a;
this.b = b;
}
internal class Comparer : IEqualityComparer<Key>
{
internal static readonly Comparer Instance = new Comparer();
private Comparer(){}
public bool Equals(Key x, Key y)
{
return x.a == y.a && x.b == y.b;
}
public int GetHashCode(Key x)
{
return (int)((x.a & 0xffff) << 16) | (x.b & 0xffff));
}
}
}
}
You're doing a lot of extra work in there, converting to and from KeyValuePair. Also, DomainEntity is a reference type, so you probably should just store references to those in the dictionary rather than having to create them from the key and value every time you look one up.
Create your dictionary as:
var _dictStore = new Dictionary<Tuple<uint, uint>, DomainEntity>();
Then:
public uint Add(DomainEntity item)
{
var key = new Tuple<uint, uint>(item.Id1, item.Id2);
_dictStore.Add(key, item);
return 0;
}
And lookup:
public DomainEntity GetDomainEntityByIds(uint articleId1, uint articleId2)
{
var key = new Tuple<uint, uint>(articleId1, articleId2);
DomainEntity value;
if (!_dictStore.TryGetValue(key, out value))
{
value = null;
}
return value;
}
I used to use the apache hashcode builder a lot
Does this exist for C#
This is my homemade builder.
Usage:
hash = new HashCodeBuilder().
Add(a).
Add(b).
Add(c).
Add(d).
GetHashCode();
It does not matter what type fields a,b,c and d are, easy to extend, no need to create array.
Source:
public sealed class HashCodeBuilder
{
private int hash = 17;
public HashCodeBuilder Add(int value)
{
unchecked
{
hash = hash * 31 + value; //see Effective Java for reasoning
// can be any prime but hash * 31 can be opimised by VM to hash << 5 - hash
}
return this;
}
public HashCodeBuilder Add(object value)
{
return Add(value != null ? value.GetHashCode() : 0);
}
public HashCodeBuilder Add(float value)
{
return Add(value.GetHashCode());
}
public HashCodeBuilder Add(double value)
{
return Add(value.GetHashCode());
}
public override int GetHashCode()
{
return hash;
}
}
Sample usage:
public sealed class Point
{
private readonly int _x;
private readonly int _y;
private readonly int _hash;
public Point(int x, int y)
{
_x = x;
_y = y;
_hash = new HashCodeBuilder().
Add(_x).
Add(_y).
GetHashCode();
}
public int X
{
get { return _x; }
}
public int Y
{
get { return _y; }
}
public override bool Equals(object obj)
{
return Equals(obj as Point);
}
public bool Equals(Point other)
{
if (other == null) return false;
return (other._x == _x) && (other._y == _y);
}
public override int GetHashCode()
{
return _hash;
}
}
I use the following:
public static int ComputeHashFrom(params object[] obj) {
ulong res = 0;
for(uint i=0;i<obj.Length;i++) {
object val = obj[i];
res += val == null ? i : (ulong)val.GetHashCode() * (1 + 2 * i);
}
return (int)(uint)(res ^ (res >> 32));
}
Using such a helper is quick, easy and reliable, but it has potential two downsides (which you aren't likely to encounter frequently, but are good to be aware of):
It can generate poor hashcodes for some distributions of params. For instance, for any int x, ComputeHashFrom(x*-3, x) == 0 - so if your objects have certain pathological properties you may get many hash code collisions resulting in poorly performing Dictionaries and HashSets. It's not likely to happen, but a type-aware hash code computation can avoid such problems more easily.
The computation of the hashcode is slower than a specialized computation could be. In particular, it involved the allocation of the params array and a loop - which quite a bit of unnecessary overhead if you've just got two members to process.
Neither of the drawbacks causes any errors merely inefficiency; and both with show up in a profiler as blips in either this method or in the internals of the hash-code consumer.
C# doesn't have a built-in HashCode builder, but you can roll your own. I recently had this precise problem and created this hashcode generator that doesn't use boxing, by using generics, and implements a modified FNV algorithm for generating the specific hash. But you could use any algorithm you'd like, like one of those in System.Security.Cryptography.
public static int GetHashCode<T>(params T[] args)
{
return args.GetArrayHashCode();
}
public static int GetArrayHashCode<T>(this T[] objects)
{
int[] data = new int[objects.Length];
for (int i = 0; i < objects.Length; i++)
{
T obj = objects[i];
data[i] = obj == null ? 1 : obj.GetHashCode();
}
return GetFnvHash(data);
}
private static int GetFnvHash(int[] data)
{
unchecked
{
const int p = 16777619;
long hash = 2166136261;
for (int i = 0; i < data.Length; i++)
{
hash = (hash ^ data[i]) * p;
}
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5;
return (int)hash;
}
}
Microsoft recently released a class to compute hashcodes. Please see https://learn.microsoft.com/en-us/dotnet/api/system.hashcode. You need to include NuGet package Microsoft.Bcl.HashCode in your project to use it.
Usage example:
using System.Collections.Generic;
public class MyClass {
public int MyVar { get; }
public string AnotherVar { get; }
public object MoreVars;
public override int GetHashCode()
=> HashCode.Combine(MyVar, AnotherVar, MoreVars);
}
Nowadays I leverage ValueTuples, ref Tuples or anonymous types:
var hash = (1, "seven").GetHashCode();
var hash2 = Tuple.Create(1, "seven").GetHashCode();
var hash3 = new { Number = 1, String = "seven" }.GetHashCode();
I believe value tuples will be fastest.
I'd like one general purpose function that could be used with any Flags style enum to see if a flag exists.
This doesn't compile, but if anyone has a suggestion, I'd appreciate it.
public static Boolean IsEnumFlagPresent<T>(T value,T lookingForFlag)
where T:enum
{
Boolean result = ((value & lookingForFlag) == lookingForFlag);
return result ;
}
No, you can't do this with C# generics. However, you could do:
public static bool IsEnumFlagPresent<T>(T value, T lookingForFlag)
where T : struct
{
int intValue = (int) (object) value;
int intLookingForFlag = (int) (object) lookingForFlag;
return ((intValue & intLookingForFlag) == intLookingForFlag);
}
This will only work for enums which have an underlying type of int, and it's somewhat inefficient because it boxes the value... but it should work.
You may want to add an execution type check that T is actually an enum type (e.g. typeof(T).BaseType == typeof(Enum))
Here's a complete program demonstrating it working:
using System;
[Flags]
enum Foo
{
A = 1,
B = 2,
C = 4,
D = 8
}
class Test
{
public static Boolean IsEnumFlagPresent<T>(T value, T lookingForFlag)
where T : struct
{
int intValue = (int) (object) value;
int intLookingForFlag = (int) (object) lookingForFlag;
return ((intValue & intLookingForFlag) == intLookingForFlag);
}
static void Main()
{
Console.WriteLine(IsEnumFlagPresent(Foo.B | Foo.C, Foo.A));
Console.WriteLine(IsEnumFlagPresent(Foo.B | Foo.C, Foo.B));
Console.WriteLine(IsEnumFlagPresent(Foo.B | Foo.C, Foo.C));
Console.WriteLine(IsEnumFlagPresent(Foo.B | Foo.C, Foo.D));
}
}
You're looking to replace one line of code with a function that wraps one line of code? I'd say to just use the one line of code...
For what its worth, I recently read that this feature will be part of .NET 4.0. Specifically, it is implemented in the Enum.HasFlag() function.
I have used this before:
public static bool In<T>(this T me, T values)
where T : struct, IConvertible
{
return (me.ToInt64(null) & values.ToInt64(null)) > 0;
}
What I like about it is you can use this clean syntax to call it since in 3.5 the compiler will can infer generic parameters.
AttributeTargets a = AttributeTargets.Class;
if (a.In(AttributeTargets.Class | AttributeTargets.Module))
{
// ...
}
You can do this without generics:
static bool ContainsFlags(Enum value, Enum flag)
{
if (Enum.GetUnderlyingType(value.GetType()) == typeof(ulong))
return (Convert.ToUInt64(value) & Convert.ToUInt64(flag)) == Convert.ToUInt64(flag);
else
return (Convert.ToInt64(value) & Convert.ToInt64(flag)) == Convert.ToInt64(flag);
}
I'm converting to Int64 in this case, which should handle every case except ulong, which is why the extra check...
Why not write an extension method for this? I did this in another post
public static class EnumerationExtensions {
public static bool Has<T>(this System.Enum type, T value) {
try {
return (((int)(object)type & (int)(object)value) == (int)(object)value);
}
catch {
return false;
}
}
//... etc...
}
//Then use it like this
bool hasValue = permissions.Has(PermissionTypes.Delete);
It could use a little refinement (since it assumes everything can be cast as an int), but it could get you started...
Worth pointing out that simply providing some static overloads for all the integral types will work so long as you know you are working with a specific enum. They won't work if the consuming code is likewise operating on where t : struct
If you need to deal with arbitrary (struct) T
You cannot currently do a fast conversion of a generically typed struct into some alternate bitwise form (i.e. roughly speaking a reinterpret_cast) without using C++/CLI
generic <typename T>
where T : value class
public ref struct Reinterpret
{
private:
const static int size = sizeof(T);
public:
static int AsInt(T t)
{
return *((Int32*) (void*) (&t));
}
}
This will then let you write:
static void IsSet<T>(T value, T flags) where T : struct
{
if (!typeof(T).IsEnum)
throw new InvalidOperationException(typeof(T).Name +" is not an enum!");
Type t = Enum.GetUnderlyingType(typeof(T));
if (t == typeof(int))
{
return (Reinterpret.AsInt(value) & Reinterpret.AsInt(flags)) != 0
}
else if (t == typeof(byte))
{
return (Reinterpret.AsByte(value) & Reinterpret.AsByte(flags)) != 0
}
// you get the idea...
}
You cannot constrain to enums. But the mathematical validity of these methods do not change if they are used with non enum types so you could allow them if you can determine that they are convertible to a struct of the relevant size.
Question long over, but here's one for reference anyway:
public static bool HasFlag<TEnum>(this TEnum enumeratedType, TEnum value)
where TEnum : struct, IComparable, IFormattable, IConvertible
{
if (!(enumeratedType is Enum))
{
throw new InvalidOperationException("Struct is not an Enum.");
}
if (typeof(TEnum).GetCustomAttributes(
typeof(FlagsAttribute), false).Length == 0)
{
throw new InvalidOperationException("Enum must use [Flags].");
}
long enumValue = enumeratedType.ToInt64(CultureInfo.InvariantCulture);
long flagValue = value.ToInt64(CultureInfo.InvariantCulture);
if ((enumValue & flagValue) == flagValue)
{
return true;
}
return false;
}
Today, you can set the c# language version to >=7.3
and use the next code as the reference:
public static class EnumExt
{
public static IEnumerable<TEnum> Explode<TEnum>(this TEnum enumValue) where TEnum : Enum
{
var res = Enum.GetValues(enumValue.GetType())
.Cast<TEnum>()
.Where(x => enumValue.HasFlag(x));
return res;
}
public static string ExplodeToString<TEnum>(this TEnum enumValue, string delimeter = ",") where TEnum : Enum
{
return string.Join(delimeter, Explode(enumValue));
}
}
Well, I don't believe there is a way to do this, as there are no constraints that apply to bitwise operators.
However... you can just cast your enum to int and do it.
public static Boolean IsEnumFlagPresent(int value,int lookingForFlag)
{
return ((value & lookingForFlag) == lookingForFlag);
}
This works, but may be confusing to someone.
below is code that benchmarks 4 different methods. results are shown in code in comment "BENCHMARK: .. nSec".
"((enum & flag) != 0)' is 10x faster than HasFlag() function. if you are in a tight loop, then i think it is best to accept it.
public static int jumpCtr=0;
public static int ctr=0;
public static TestFlags gTestFlags = TestFlags.C;
[Flags] public enum TestFlags { A=1<<1, B=1<<2, C=1<<3 }
public static void Jump() { jumpCtr++; gTestFlags = (gTestFlags == TestFlags.B) ? TestFlags.C : TestFlags.B; }
// IsEnumFlagPresent() https://stackoverflow.com/questions/987607/c-flags-enum-generic-function-to-look-for-a-flag
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool HasFlag_Faster<T>(T value, T lookingForFlag)
where T : struct
{
int intValue = (int) (object) value;
int intLookingForFlag = (int) (object) lookingForFlag;
return ((intValue & intLookingForFlag) != 0);
}
// IsEnumFlagPresent() https://stackoverflow.com/questions/987607/c-flags-enum-generic-function-to-look-for-a-flag
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool HasFlag_Faster_Integer(int intValue, int intLookingForFlag)
{
return ((intValue & intLookingForFlag) != 0);
}
public static void Benchmark_HasFlag( )
{
if ( ! hwDvr._weAreOnGswCpu) { return; }
DateTime timer = DateTime.Now;
string a, b, c, d, e;
double base_nSecPerLoop, b_nSecPerLoop, c_nSecPerLoop, d_nSecPerLoop, e_nSecPerLoop;
int numOfLoops = (int) 1.0e6;
// ------------------------------------------------------
for (int i=0; i<numOfLoops;i++) {
Jump();
}
a = BenchMarkSystem_Helper.SimpleTimer_Loops( ref timer, numOfLoops, out base_nSecPerLoop);
// ------------------------------------------------------
// BENCHMARK: 50 nSec
for (int i=0; i<numOfLoops;i++) {
if (gTestFlags.HasFlag((TestFlags) TestFlags.C)) {
ctr++;
}
Jump();
}
b = BenchMarkSystem_Helper.SimpleTimer_Loops( ref timer, numOfLoops, out b_nSecPerLoop );
double b_diff = b_nSecPerLoop - base_nSecPerLoop;
// ------------------------------------------------------
// BENCHMARK: 3 nSec
for (int i=0; i<numOfLoops;i++) {
if ((gTestFlags & TestFlags.C) != 0) {
ctr++;
}
Jump();
}
c = BenchMarkSystem_Helper.SimpleTimer_Loops( ref timer, numOfLoops, out c_nSecPerLoop );
double c_diff = c_nSecPerLoop - base_nSecPerLoop;
// ------------------------------------------------------
// BENCHMARK: 64 nSec
for (int i=0; i<numOfLoops;i++) {
if (HasFlag_Faster<TestFlags>(value:gTestFlags, lookingForFlag: TestFlags.C)) {
ctr++;
}
Jump();
}
d = BenchMarkSystem_Helper.SimpleTimer_Loops( ref timer, numOfLoops, out d_nSecPerLoop );
double d_diff = d_nSecPerLoop - base_nSecPerLoop;
// ------------------------------------------------------
// BENCHMARK: 14 nSec
for (int i=0; i<numOfLoops;i++) {
if (HasFlag_Faster_Integer((int)gTestFlags, (int)TestFlags.C)) {
ctr++;
}
Jump();
}
e = BenchMarkSystem_Helper.SimpleTimer_Loops( ref timer, numOfLoops, out e_nSecPerLoop );
double e_diff = e_nSecPerLoop - base_nSecPerLoop;
int brkPt=0;
}
How do I cast an int to an enum in C#?
From an int:
YourEnum foo = (YourEnum)yourInt;
From a string:
YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);
// The foo.ToString().Contains(",") check is necessary for
// enumerations marked with a [Flags] attribute.
if (!Enum.IsDefined(typeof(YourEnum), foo) && !foo.ToString().Contains(","))
{
throw new InvalidOperationException(
$"{yourString} is not an underlying value of the YourEnum enumeration."
);
}
From a number:
YourEnum foo = (YourEnum)Enum.ToObject(typeof(YourEnum), yourInt);
Just cast it:
MyEnum e = (MyEnum)3;
Check if it's in range using Enum.IsDefined:
if (Enum.IsDefined(typeof(MyEnum), 3)) { ... }
Alternatively, use an extension method instead of a one-liner:
public static T ToEnum<T>(this string enumString)
{
return (T) Enum.Parse(typeof (T), enumString);
}
Usage:
Color colorEnum = "Red".ToEnum<Color>();
OR
string color = "Red";
var colorEnum = color.ToEnum<Color>();
I think to get a complete answer, people have to know how enums work internally in .NET.
How stuff works
An enum in .NET is a structure that maps a set of values (fields) to a basic type (the default is int). However, you can actually choose the integral type that your enum maps to:
public enum Foo : short
In this case the enum is mapped to the short data type, which means it will be stored in memory as a short and will behave as a short when you cast and use it.
If you look at it from a IL point of view, a (normal, int) enum looks like this:
.class public auto ansi serializable sealed BarFlag extends System.Enum
{
.custom instance void System.FlagsAttribute::.ctor()
.custom instance void ComVisibleAttribute::.ctor(bool) = { bool(true) }
.field public static literal valuetype BarFlag AllFlags = int32(0x3fff)
.field public static literal valuetype BarFlag Foo1 = int32(1)
.field public static literal valuetype BarFlag Foo2 = int32(0x2000)
// and so on for all flags or enum values
.field public specialname rtspecialname int32 value__
}
What should get your attention here is that the value__ is stored separately from the enum values. In the case of the enum Foo above, the type of value__ is int16. This basically means that you can store whatever you want in an enum, as long as the types match.
At this point I'd like to point out that System.Enum is a value type, which basically means that BarFlag will take up 4 bytes in memory and Foo will take up 2 -- e.g. the size of the underlying type (it's actually more complicated than that, but hey...).
The answer
So, if you have an integer that you want to map to an enum, the runtime only has to do 2 things: copy the 4 bytes and name it something else (the name of the enum). Copying is implicit because the data is stored as value type - this basically means that if you use unmanaged code, you can simply interchange enums and integers without copying data.
To make it safe, I think it's a best practice to know that the underlying types are the same or implicitly convertible and to ensure the enum values exist (they aren't checked by default!).
To see how this works, try the following code:
public enum MyEnum : int
{
Foo = 1,
Bar = 2,
Mek = 5
}
static void Main(string[] args)
{
var e1 = (MyEnum)5;
var e2 = (MyEnum)6;
Console.WriteLine("{0} {1}", e1, e2);
Console.ReadLine();
}
Note that casting to e2 also works! From the compiler perspective above this makes sense: the value__ field is simply filled with either 5 or 6 and when Console.WriteLine calls ToString(), the name of e1 is resolved while the name of e2 is not.
If that's not what you intended, use Enum.IsDefined(typeof(MyEnum), 6) to check if the value you are casting maps to a defined enum.
Also note that I'm explicit about the underlying type of the enum, even though the compiler actually checks this. I'm doing this to ensure I don't run into any surprises down the road. To see these surprises in action, you can use the following code (actually I've seen this happen a lot in database code):
public enum MyEnum : short
{
Mek = 5
}
static void Main(string[] args)
{
var e1 = (MyEnum)32769; // will not compile, out of bounds for a short
object o = 5;
var e2 = (MyEnum)o; // will throw at runtime, because o is of type int
Console.WriteLine("{0} {1}", e1, e2);
Console.ReadLine();
}
Take the following example:
int one = 1;
MyEnum e = (MyEnum)one;
I am using this piece of code to cast int to my enum:
if (typeof(YourEnum).IsEnumDefined(valueToCast)) return (YourEnum)valueToCast;
else { //handle it here, if its not defined }
I find it the best solution.
Below is a nice utility class for Enums
public static class EnumHelper
{
public static int[] ToIntArray<T>(T[] value)
{
int[] result = new int[value.Length];
for (int i = 0; i < value.Length; i++)
result[i] = Convert.ToInt32(value[i]);
return result;
}
public static T[] FromIntArray<T>(int[] value)
{
T[] result = new T[value.Length];
for (int i = 0; i < value.Length; i++)
result[i] = (T)Enum.ToObject(typeof(T),value[i]);
return result;
}
internal static T Parse<T>(string value, T defaultValue)
{
if (Enum.IsDefined(typeof(T), value))
return (T) Enum.Parse(typeof (T), value);
int num;
if(int.TryParse(value,out num))
{
if (Enum.IsDefined(typeof(T), num))
return (T)Enum.ToObject(typeof(T), num);
}
return defaultValue;
}
}
For numeric values, this is safer as it will return an object no matter what:
public static class EnumEx
{
static public bool TryConvert<T>(int value, out T result)
{
result = default(T);
bool success = Enum.IsDefined(typeof(T), value);
if (success)
{
result = (T)Enum.ToObject(typeof(T), value);
}
return success;
}
}
If you're ready for the 4.0 .NET Framework, there's a new Enum.TryParse() function that's very useful and plays well with the [Flags] attribute. See Enum.TryParse Method (String, TEnum%)
Sometimes you have an object to the MyEnum type. Like
var MyEnumType = typeof(MyEnum);
Then:
Enum.ToObject(typeof(MyEnum), 3)
If you have an integer that acts as a bitmask and could represent one or more values in a [Flags] enumeration, you can use this code to parse the individual flag values into a list:
for (var flagIterator = 0; flagIterator < 32; flagIterator++)
{
// Determine the bit value (1,2,4,...,Int32.MinValue)
int bitValue = 1 << flagIterator;
// Check to see if the current flag exists in the bit mask
if ((intValue & bitValue) != 0)
{
// If the current flag exists in the enumeration, then we can add that value to the list
// if the enumeration has that flag defined
if (Enum.IsDefined(typeof(MyEnum), bitValue))
Console.WriteLine((MyEnum)bitValue);
}
}
Note that this assumes that the underlying type of the enum is a signed 32-bit integer. If it were a different numerical type, you'd have to change the hardcoded 32 to reflect the bits in that type (or programatically derive it using Enum.GetUnderlyingType())
This is an flags enumeration aware safe convert method:
public static bool TryConvertToEnum<T>(this int instance, out T result)
where T: Enum
{
var enumType = typeof (T);
var success = Enum.IsDefined(enumType, instance);
if (success)
{
result = (T)Enum.ToObject(enumType, instance);
}
else
{
result = default(T);
}
return success;
}
To convert a string to ENUM or int to ENUM constant we need to use Enum.Parse function. Here is a youtube video https://www.youtube.com/watch?v=4nhx4VwdRDk which actually demonstrate's with string and the same applies for int.
The code goes as shown below where "red" is the string and "MyColors" is the color ENUM which has the color constants.
MyColors EnumColors = (MyColors)Enum.Parse(typeof(MyColors), "Red");
Slightly getting away from the original question, but I found an answer to Stack Overflow question Get int value from enum useful. Create a static class with public const int properties, allowing you to easily collect together a bunch of related int constants, and then not have to cast them to int when using them.
public static class Question
{
public static readonly int Role = 2;
public static readonly int ProjectFunding = 3;
public static readonly int TotalEmployee = 4;
public static readonly int NumberOfServers = 5;
public static readonly int TopBusinessConcern = 6;
}
Obviously, some of the enum type functionality will be lost, but for storing a bunch of database id constants, it seems like a pretty tidy solution.
The following is a slightly better extension method:
public static string ToEnumString<TEnum>(this int enumValue)
{
var enumString = enumValue.ToString();
if (Enum.IsDefined(typeof(TEnum), enumValue))
{
enumString = ((TEnum) Enum.ToObject(typeof (TEnum), enumValue)).ToString();
}
return enumString;
}
This parses integers or strings to a target enum with partial matching in .NET 4.0 using generics like in Tawani's utility class. I am using it to convert command-line switch variables which may be incomplete. Since an enum cannot be null, you should logically provide a default value. It can be called like this:
var result = EnumParser<MyEnum>.Parse(valueToParse, MyEnum.FirstValue);
Here's the code:
using System;
public class EnumParser<T> where T : struct
{
public static T Parse(int toParse, T defaultVal)
{
return Parse(toParse + "", defaultVal);
}
public static T Parse(string toParse, T defaultVal)
{
T enumVal = defaultVal;
if (defaultVal is Enum && !String.IsNullOrEmpty(toParse))
{
int index;
if (int.TryParse(toParse, out index))
{
Enum.TryParse(index + "", out enumVal);
}
else
{
if (!Enum.TryParse<T>(toParse + "", true, out enumVal))
{
MatchPartialName(toParse, ref enumVal);
}
}
}
return enumVal;
}
public static void MatchPartialName(string toParse, ref T enumVal)
{
foreach (string member in enumVal.GetType().GetEnumNames())
{
if (member.ToLower().Contains(toParse.ToLower()))
{
if (Enum.TryParse<T>(member + "", out enumVal))
{
break;
}
}
}
}
}
FYI: The question was about integers, which nobody mentioned will also explicitly convert in Enum.TryParse()
From a string: (Enum.Parse is out of Date, use Enum.TryParse)
enum Importance
{}
Importance importance;
if (Enum.TryParse(value, out importance))
{
}
You should build in some type matching relaxation to be more robust.
public static T ToEnum<T>(dynamic value)
{
if (value == null)
{
// default value of an enum is the object that corresponds to
// the default value of its underlying type
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/default-values-table
value = Activator.CreateInstance(Enum.GetUnderlyingType(typeof(T)));
}
else if (value is string name)
{
return (T)Enum.Parse(typeof(T), name);
}
return (T)Enum.ToObject(typeof(T),
Convert.ChangeType(value, Enum.GetUnderlyingType(typeof(T))));
}
Test Case
[Flags]
public enum A : uint
{
None = 0,
X = 1 < 0,
Y = 1 < 1
}
static void Main(string[] args)
{
var value = EnumHelper.ToEnum<A>(7m);
var x = value.HasFlag(A.X); // true
var y = value.HasFlag(A.Y); // true
var value2 = EnumHelper.ToEnum<A>("X");
var value3 = EnumHelper.ToEnum<A>(null);
Console.ReadKey();
}
Here's an extension method that casts Int32 to Enum.
It honors bitwise flags even when the value is higher than the maximum possible. For example if you have an enum with possibilities 1, 2, and 4, but the int is 9, it understands that as 1 in absence of an 8. This lets you make data updates ahead of code updates.
public static TEnum ToEnum<TEnum>(this int val) where TEnum : struct, IComparable, IFormattable, IConvertible
{
if (!typeof(TEnum).IsEnum)
{
return default(TEnum);
}
if (Enum.IsDefined(typeof(TEnum), val))
{//if a straightforward single value, return that
return (TEnum)Enum.ToObject(typeof(TEnum), val);
}
var candidates = Enum
.GetValues(typeof(TEnum))
.Cast<int>()
.ToList();
var isBitwise = candidates
.Select((n, i) => {
if (i < 2) return n == 0 || n == 1;
return n / 2 == candidates[i - 1];
})
.All(y => y);
var maxPossible = candidates.Sum();
if (
Enum.TryParse(val.ToString(), out TEnum asEnum)
&& (val <= maxPossible || !isBitwise)
){//if it can be parsed as a bitwise enum with multiple flags,
//or is not bitwise, return the result of TryParse
return asEnum;
}
//If the value is higher than all possible combinations,
//remove the high imaginary values not accounted for in the enum
var excess = Enumerable
.Range(0, 32)
.Select(n => (int)Math.Pow(2, n))
.Where(n => n <= val && n > 0 && !candidates.Contains(n))
.Sum();
return Enum.TryParse((val - excess).ToString(), out asEnum) ? asEnum : default(TEnum);
}
The easy and clear way for casting an int to enum in C#:
public class Program
{
public enum Color : int
{
Blue = 0,
Black = 1,
Green = 2,
Gray = 3,
Yellow = 4
}
public static void Main(string[] args)
{
// From string
Console.WriteLine((Color) Enum.Parse(typeof(Color), "Green"));
// From int
Console.WriteLine((Color)2);
// From number you can also
Console.WriteLine((Color)Enum.ToObject(typeof(Color), 2));
}
}
For string, you can do the following:
var result = Enum.TryParse(typeof(MyEnum), yourString, out yourEnum)
And make sure to check the result to determine if the conversion failed.
For int, you can do the following:
MyEnum someValue = (MyEnum)myIntValue;
In my case, I needed to return the enum from a WCF service. I also needed a friendly name, not just the enum.ToString().
Here's my WCF Class.
[DataContract]
public class EnumMember
{
[DataMember]
public string Description { get; set; }
[DataMember]
public int Value { get; set; }
public static List<EnumMember> ConvertToList<T>()
{
Type type = typeof(T);
if (!type.IsEnum)
{
throw new ArgumentException("T must be of type enumeration.");
}
var members = new List<EnumMember>();
foreach (string item in System.Enum.GetNames(type))
{
var enumType = System.Enum.Parse(type, item);
members.Add(
new EnumMember() { Description = enumType.GetDescriptionValue(), Value = ((IConvertible)enumType).ToInt32(null) });
}
return members;
}
}
Here's the Extension method that gets the Description from the Enum.
public static string GetDescriptionValue<T>(this T source)
{
FieldInfo fileInfo = source.GetType().GetField(source.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fileInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
return attributes[0].Description;
}
else
{
return source.ToString();
}
}
Implementation:
return EnumMember.ConvertToList<YourType>();
It can help you to convert any input data to user desired enum. Suppose you have an enum like below which by default int. Please add a Default value at first of your enum. Which is used at helpers medthod when there is no match found with input value.
public enum FriendType
{
Default,
Audio,
Video,
Image
}
public static class EnumHelper<T>
{
public static T ConvertToEnum(dynamic value)
{
var result = default(T);
var tempType = 0;
//see Note below
if (value != null &&
int.TryParse(value.ToString(), out tempType) &&
Enum.IsDefined(typeof(T), tempType))
{
result = (T)Enum.ToObject(typeof(T), tempType);
}
return result;
}
}
N.B: Here I try to parse value into int, because enum is by default int
If you define enum like this which is byte type.
public enum MediaType : byte
{
Default,
Audio,
Video,
Image
}
You need to change parsing at helper method from
int.TryParse(value.ToString(), out tempType)
to
byte.TryParse(value.ToString(), out tempType)
I check my method for following inputs
EnumHelper<FriendType>.ConvertToEnum(null);
EnumHelper<FriendType>.ConvertToEnum("");
EnumHelper<FriendType>.ConvertToEnum("-1");
EnumHelper<FriendType>.ConvertToEnum("6");
EnumHelper<FriendType>.ConvertToEnum("");
EnumHelper<FriendType>.ConvertToEnum("2");
EnumHelper<FriendType>.ConvertToEnum(-1);
EnumHelper<FriendType>.ConvertToEnum(0);
EnumHelper<FriendType>.ConvertToEnum(1);
EnumHelper<FriendType>.ConvertToEnum(9);
sorry for my english
Different ways to cast to and from Enum
enum orientation : byte
{
north = 1,
south = 2,
east = 3,
west = 4
}
class Program
{
static void Main(string[] args)
{
orientation myDirection = orientation.north;
Console.WriteLine(“myDirection = {0}”, myDirection); //output myDirection =north
Console.WriteLine((byte)myDirection); //output 1
string strDir = Convert.ToString(myDirection);
Console.WriteLine(strDir); //output north
string myString = “north”; //to convert string to Enum
myDirection = (orientation)Enum.Parse(typeof(orientation),myString);
}
}
I don't know anymore where I get the part of this enum extension, but it is from stackoverflow. I am sorry for this! But I took this one and modified it for enums with Flags.
For enums with Flags I did this:
public static class Enum<T> where T : struct
{
private static readonly IEnumerable<T> All = Enum.GetValues(typeof (T)).Cast<T>();
private static readonly Dictionary<int, T> Values = All.ToDictionary(k => Convert.ToInt32(k));
public static T? CastOrNull(int value)
{
T foundValue;
if (Values.TryGetValue(value, out foundValue))
{
return foundValue;
}
// For enums with Flags-Attribut.
try
{
bool isFlag = typeof(T).GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0;
if (isFlag)
{
int existingIntValue = 0;
foreach (T t in Enum.GetValues(typeof(T)))
{
if ((value & Convert.ToInt32(t)) > 0)
{
existingIntValue |= Convert.ToInt32(t);
}
}
if (existingIntValue == 0)
{
return null;
}
return (T)(Enum.Parse(typeof(T), existingIntValue.ToString(), true));
}
}
catch (Exception)
{
return null;
}
return null;
}
}
Example:
[Flags]
public enum PetType
{
None = 0, Dog = 1, Cat = 2, Fish = 4, Bird = 8, Reptile = 16, Other = 32
};
integer values
1=Dog;
13= Dog | Fish | Bird;
96= Other;
128= Null;
You simply use Explicit conversion Cast int to enum or enum to int
class Program
{
static void Main(string[] args)
{
Console.WriteLine((int)Number.three); //Output=3
Console.WriteLine((Number)3);// Outout three
Console.Read();
}
public enum Number
{
Zero = 0,
One = 1,
Two = 2,
three = 3
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace SamplePrograme
{
public class Program
{
public enum Suit : int
{
Spades = 0,
Hearts = 1,
Clubs = 2,
Diamonds = 3
}
public static void Main(string[] args)
{
//from string
Console.WriteLine((Suit) Enum.Parse(typeof(Suit), "Clubs"));
//from int
Console.WriteLine((Suit)1);
//From number you can also
Console.WriteLine((Suit)Enum.ToObject(typeof(Suit) ,1));
}
}
}
I prefer a short way using a nullable enum type variable.
var enumValue = (MyEnum?)enumInt;
if (!enumValue.HasValue)
{
throw new ArgumentException(nameof(enumValue));
}
You just do like below:
int intToCast = 1;
TargetEnum f = (TargetEnum) intToCast ;
To make sure that you only cast the right values and that you can throw an exception otherwise:
int intToCast = 1;
if (Enum.IsDefined(typeof(TargetEnum), intToCast ))
{
TargetEnum target = (TargetEnum)intToCast ;
}
else
{
// Throw your exception.
}
Note that using IsDefined is costly and even more than just casting, so it depends on your implementation to decide to use it or not.
You can use an extension method.
public static class Extensions
{
public static T ToEnum<T>(this string data) where T : struct
{
if (!Enum.TryParse(data, true, out T enumVariable))
{
if (Enum.IsDefined(typeof(T), enumVariable))
{
return enumVariable;
}
}
return default;
}
public static T ToEnum<T>(this int data) where T : struct
{
return (T)Enum.ToObject(typeof(T), data);
}
}
Use it like the below code:
Enum:
public enum DaysOfWeeks
{
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
Sunday = 7,
}
Usage:
string Monday = "Mon";
int Wednesday = 3;
var Mon = Monday.ToEnum<DaysOfWeeks>();
var Wed = Wednesday.ToEnum<DaysOfWeeks>();