Get Enum Value - CSharp System

CSharp examples for System:Enum

Description

Get Enum Value

Demo Code


using System.Xml.Serialization;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.IO;//from   w w w  .  j  a v  a2 s. c o  m
using System.Collections.Generic;
using System;

public class Main{
        public static T GetEnumValue<T>(String value)
        {
            var found = typeof(T).GetEnumValues().Cast<Enum>().FirstOrDefault(x => GetNameValue(x) == value);
            T result = default(T);

            if (found is T)
            {
                result = (T)(object)found;
            }

            return result;
        }
}

Related Tutorials