What is the size of an enum in C?

Taken from the current C Standard (C99): http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf 6.7.2.2 Enumeration specifiers […] Constraints The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int. […] Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. … Read more

JavaScriptSerializer – JSON serialization of enum as string

I have found that Json.NET provides the exact functionality I’m looking for with a StringEnumConverter attribute: using Newtonsoft.Json; using Newtonsoft.Json.Converters; [JsonConverter(typeof(StringEnumConverter))] public Gender Gender { get; set; } More details at available on StringEnumConverter documentation. There are other places to configure this converter more globally: enum itself if you want enum always be serialized/deserialized as … Read more

How to bind RadioButtons to an enum?

You can further simplify the accepted answer. Instead of typing out the enums as strings in xaml and doing more work in your converter than needed, you can explicitly pass in the enum value instead of a string representation, and as CrimsonX commented, errors get thrown at compile time rather than runtime: ConverterParameter={x:Static local:YourEnumType.Enum1} <StackPanel> … Read more

Getting attributes of Enum’s value

This should do what you need. try { var enumType = typeof(FunkyAttributesEnum); var memberInfos = enumType.GetMember(FunkyAttributesEnum.NameWithoutSpaces1.ToString()); var enumValueMemberInfo = memberInfos.FirstOrDefault(m => m.DeclaringType == enumType); var valueAttributes = enumValueMemberInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); var description = ((DescriptionAttribute)valueAttributes[0]).Description; } catch { return FunkyAttributesEnum.NameWithoutSpaces1.ToString() }

Create Generic method constraining T to an Enum

Since Enum Type implements IConvertible interface, a better implementation should be something like this: public T GetEnumFromString<T>(string value) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new ArgumentException(“T must be an enumerated type”); } //… } This will still permit passing of value types implementing IConvertible. The chances are rare though.

Convert a string to an enum in C#

In .NET Core and .NET Framework ≥4.0 there is a generic parse method: Enum.TryParse(“Active”, out StatusEnum myStatus); This also includes C#7’s new inline out variables, so this does the try-parse, conversion to the explicit enum type and initialises+populates the myStatus variable. If you have access to C#7 and the latest .NET this is the best … Read more