String representation of an Enum

Try type-safe-enum pattern. public sealed class AuthenticationMethod { private readonly String name; private readonly int value; public static readonly AuthenticationMethod FORMS = new AuthenticationMethod (1, “FORMS”); public static readonly AuthenticationMethod WINDOWSAUTHENTICATION = new AuthenticationMethod (2, “WINDOWS”); public static readonly AuthenticationMethod SINGLESIGNON = new AuthenticationMethod (3, “SSN”); private AuthenticationMethod(int value, String name){ this.name = name; this.value … Read more

What is a typedef enum in Objective-C?

Three things are being declared here: an anonymous enumerated type is declared, ShapeType is being declared a typedef for that anonymous enumeration, and the three names kCircle, kRectangle, and kOblateSpheroid are being declared as integral constants. Let’s break that down. In the simplest case, an enumeration can be declared as enum tagname { … }; … Read more

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

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

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.

What does the [Flags] Enum Attribute mean in C#?

The [Flags] attribute should be used whenever the enumerable represents a collection of possible values, rather than a single value. Such collections are often used with bitwise operators, for example: var allowedColors = MyColor.Red | MyColor.Green | MyColor.Blue; Note that the [Flags] attribute doesn’t enable this by itself – all it does is allow a … Read more

How to loop through all enum values in C#? [duplicate]

Yes you can use the ‍GetValue‍‍‍s method: var values = Enum.GetValues(typeof(Foos)); Or the typed version: var values = Enum.GetValues(typeof(Foos)).Cast<Foos>(); I long ago added a helper function to my private library for just such an occasion: public static class EnumUtil { public static IEnumerable<T> GetValues<T>() { return Enum.GetValues(typeof(T)).Cast<T>(); } } Usage: var values = EnumUtil.GetValues<Foos>();

Get int value from enum in C#

Just cast the enum, e.g. int something = (int) Question.Role; The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int. However, as cecilphillip points out, enums can have different underlying types. If an enum is declared as a uint, long, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)