CSharp examples for System:Enum
Searches an enum for a value by name and returns it.
using System.Xml.Linq; using System.Globalization; using System;/* w w w . j ava 2 s. co m*/ public class Main{ /// <summary> /// Searches an enum for a value by name and returns it. /// </summary> /// <typeparam name="T">The type of the enum to search through.</typeparam> /// <param name="name">The name of the value to search for.</param> /// <param name="result">The variable to store the result to if a match is found.</param> /// <returns>true if the value was found and stored in result.</returns> private static bool FindEnumValue<T> (string name, out T result) { // Use reflection to scan the enum and find the first match string[] names = Enum.GetNames(typeof(T)); var index = Array.IndexOf(names, name); if ( index >= 0 ) { T[] values = (T[]) Enum.GetValues(typeof(T)); result = values[index]; return true; } result = default(T); return false; } }