1. The most common method
public enum Suits
{
Spades,
Hearts,
Clubs,
Diamonds,
NumSuits
}
the public void PrintAllSuits ()
{
foreach (string the name in Enum.GetNames (typeof (Suits)))
{
System.Console.WriteLine (suit);
}
}
/ / Z 2012-2-16 17:49:28 PM IS2120 @ http://www.alychitech.com
(2) Extension Methods (more general, more convenient)
public static class EnumExtensions
{
/ / / <Summary>
/ / / Gets all items for an enum value.
/ / / </ Summary>
/ / / <Typeparam name = “T”> </ typeparam>
/ / / <Param name = “value”> The value. </ param>
/ / / <Returns> </ returns>
public static IEnumerable <T> GetAllItems <T> (this Enum value)
{
foreach (object item in Enum.GetValues ??(typeof (T)))
{
yield return (T) item;
}
}
/ / / <Summary>
/ / / Gets all items for an enum type.
/ / / </ Summary>
/ / / <Typeparam name = “T”> </ typeparam>
/ / / <Param name = “value”> The value. </ param>
/ / / <Returns> </ returns>
public static IEnumerable <T> GetAllItems <T> () where T: struct
{
foreach (object item in Enum.GetValues ??(typeof (T)))
{
yield return (T) item;
}
}
/ / / <Summary>
/ / / Gets all combined items from an enum value.
/ / / </ Summary>
/ / / <Typeparam name = “T”> </ typeparam>
/ / / <Param name = “value”> The value. </ param>
/ / / <Returns> </ returns>
/ / / <Example>
/ / / Displays ValueA and ValueB.
/ / / <Code>
/ / / EnumExample dummy = EnumExample.Combi;
/ / / Foreach (var item in dummy.GetAllSelectedItems <EnumExample> ())
/ / / {
/ / / Console.WriteLine (item);
/ / /}
/ / / </ Code>
/ / / </ Example>
public static IEnumerable <T> GetAllSelectedItems <T> (this Enum value)
{
int valueAsInt = Convert.ToInt32 (value, CultureInfo.InvariantCulture);
foreach (object item in Enum.GetValues ??(typeof (T)))
{
int itemAsInt = Convert.ToInt32 (item, CultureInfo.InvariantCulture);
if (itemAsInt == (valueAsInt & itemAsInt))
{
yield return (T) item;
}
}
}
/ / / <Summary>
/ / / Determines whether the enum value contains a specific value.
/ / / </ Summary>
/ / / <Param name = “value”> The value. </ param>
/ / / <Param name = “request”> The request. </ param>
/ / / <Returns>
/ / / <C> true </ c> if value contains the specified value; otherwise, <c> false </ c>.
/ / / </ Returns>
/ / / <Example>
/ / / <Code>
/ / / EnumExample dummy = EnumExample.Combi;
/ / / If (dummy.Contains <EnumExample> (EnumExample.ValueA))
/ / / {
/ / / Console.WriteLine (“dummy contains EnumExample.ValueA”);
/ / /}
/ / / </ Code>
/ / / </ Example>
public static bool Contains <T> (this Enum value, T request)
{
int valueAsInt = Convert.ToInt32 (value, CultureInfo.InvariantCulture);
int requestAsInt = Convert.ToInt32 (request, CultureInfo.InvariantCulture);
if (requestAsInt == (valueAsInt & requestAsInt))
{
return true;
}
return false;
}
}
/ / Z 2012-2-16 17:51:39 PM is2120 @ http://www.alychitech.com
/ / Z The enum itself must be decorated with the FlagsAttribute
[Flags]
public enum EnumExample
{
ValueA = 1,
ValueB = 2
ValueC = 4
ValueD = 8,
Combi = ValueA | ValueB
}
(3) in the compact framework does not support Enum.GetValues ??(/ / Z 2012-2-16 17:47:28 PM the IS2120 _AT_ http://www.alychitech.com)
The. NET compact framework does not support Enum.GetValues. Here’s a good workaround from Ideas 2.0: Enum.GetValues ??in Compact Framework:
/ / Z 2012-2-16 17:49:28 PM IS2120 @ http://www.alychitech.com
public IEnumerable <Enum> GetValues ??(Enum enumeration)
{
List <Enum> enumerations = new List <Enum> ();
foreach (FieldInfo fieldInfo in enumeration.GetType (). GetFields (
BindingFlags.Static | BindingFlags.Public))
{
enumerations.Add ((Enum) fieldInfo.GetValue (enumeration));
}
return enumerations;
}