Skip to main content
Copy edited (e.g. ref. <http://en.wikipedia.org/wiki/Language_Integrated_Query>).
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

If you need speed and type checking at build and run time, this helper method is better than using LinqLINQ to cast each element:

public static T[] GetEnumValues<T>() where T : struct, IComparable, IFormattable, IConvertible
{
    if (typeof(T).BaseType != typeof(Enum))
    {
        throw new ArgumentException(string.Format("{0} is not of type System.Enum", typeof(T)));
    }
    return Enum.GetValues(typeof(T)) as T[];
}

andAnd you can use it like below:

static readonly YourEnum[] _values = GetEnumValues<YourEnum>();

ofOf course you can return IEnumerable<T>, but that buys you nothing here.

If you need speed and type checking at build and run time, this helper method is better than using Linq to cast each element

public static T[] GetEnumValues<T>() where T : struct, IComparable, IFormattable, IConvertible
{
   if (typeof(T).BaseType != typeof(Enum))
   {
      throw new ArgumentException(string.Format("{0} is not of type System.Enum", typeof(T)));
   }
   return Enum.GetValues(typeof(T)) as T[];
}

and you can use it like below:

static readonly YourEnum[] _values = GetEnumValues<YourEnum>();

of course you can return IEnumerable<T>, but that buys you nothing here

If you need speed and type checking at build and run time, this helper method is better than using LINQ to cast each element:

public static T[] GetEnumValues<T>() where T : struct, IComparable, IFormattable, IConvertible
{
    if (typeof(T).BaseType != typeof(Enum))
    {
        throw new ArgumentException(string.Format("{0} is not of type System.Enum", typeof(T)));
    }
    return Enum.GetValues(typeof(T)) as T[];
}

And you can use it like below:

static readonly YourEnum[] _values = GetEnumValues<YourEnum>();

Of course you can return IEnumerable<T>, but that buys you nothing here.

deleted 42 characters in body
Source Link
Ian Boyd
  • 259.3k
  • 271
  • 920
  • 1.3k

If you need speed and type checking at build and run time, this helper method is better than using Linq to cast each element

        public static T[] GetEnumValues<T>() where T : struct, IComparable, IFormattable, IConvertible
    {
        if (typeof(T).BaseType != typeof(Enum))
        {
            throw new ArgumentException(string.Format("{0} is not of type System.Enum", typeof(T)));
        }
        return Enum.GetValues(typeof(T)) as T[];
    }

and you can use it like below:

static readonly YourEnum[] _values = GetEnumValues<YourEnum>();

of course you can return IEnumerable<T>, but that buys you nothing here

If you need speed and type checking at build and run time, this helper method is better than using Linq to cast each element

        public static T[] GetEnumValues<T>() where T : struct, IComparable, IFormattable, IConvertible
    {
        if (typeof(T).BaseType != typeof(Enum))
        {
            throw new ArgumentException(string.Format("{0} is not of type System.Enum", typeof(T)));
        }
        return Enum.GetValues(typeof(T)) as T[];
    }

and you can use it like below:

static readonly YourEnum[] _values = GetEnumValues<YourEnum>();

of course you can return IEnumerable<T>, but that buys you nothing here

If you need speed and type checking at build and run time, this helper method is better than using Linq to cast each element

public static T[] GetEnumValues<T>() where T : struct, IComparable, IFormattable, IConvertible
{
   if (typeof(T).BaseType != typeof(Enum))
   {
      throw new ArgumentException(string.Format("{0} is not of type System.Enum", typeof(T)));
   }
   return Enum.GetValues(typeof(T)) as T[];
}

and you can use it like below:

static readonly YourEnum[] _values = GetEnumValues<YourEnum>();

of course you can return IEnumerable<T>, but that buys you nothing here

more
Source Link
dmihailescu
  • 1.7k
  • 17
  • 15

If you need speed and type checking at build and run time, this helper method is better than using IEnumerable in foreach loopsLinq to cast each element

        public static T[] GetEnumValues<T>() where T : struct, IComparable, IFormattable, IConvertible
    {
        if (typeof(T).BaseType != typeof(Enum))
        {
            throw new ArgumentException(string.Format("{0} is not of type System.Enum", typeof(T)));
        }
        return Enum.GetValues(typeof(T)) as T[];
    }

and you can use it like below:

static readonly YourEnum[] _values = ProjectUtils.GetEnumValues<YourEnum>();

of course you can return IEnumerable<T>, but that buys you nothing here

If you need speed and type checking at build and run time, this helper method is better than using IEnumerable in foreach loops

        public static T[] GetEnumValues<T>() where T : struct, IComparable, IFormattable, IConvertible
    {
        if (typeof(T).BaseType != typeof(Enum))
        {
            throw new ArgumentException(string.Format("{0} is not of type System.Enum", typeof(T)));
        }
        return Enum.GetValues(typeof(T)) as T[];
    }

and you can use it like below:

static readonly YourEnum[] _values = ProjectUtils.GetEnumValues<YourEnum>();

If you need speed and type checking at build and run time, this helper method is better than using Linq to cast each element

        public static T[] GetEnumValues<T>() where T : struct, IComparable, IFormattable, IConvertible
    {
        if (typeof(T).BaseType != typeof(Enum))
        {
            throw new ArgumentException(string.Format("{0} is not of type System.Enum", typeof(T)));
        }
        return Enum.GetValues(typeof(T)) as T[];
    }

and you can use it like below:

static readonly YourEnum[] _values = GetEnumValues<YourEnum>();

of course you can return IEnumerable<T>, but that buys you nothing here

Source Link
dmihailescu
  • 1.7k
  • 17
  • 15
Loading