2

I have a method such as:

public void DoIt(params MyEnum[] channels)
{

}

Is there a way to get the int values of the enums in the params?

I tried var myInts = channels.Select(x => x.Value) but no luck

Jon
  • 37,339
  • 77
  • 225
  • 374

7 Answers7

7
var myInts = channels.Cast<int>();
Justin Niessner
  • 236,029
  • 38
  • 403
  • 530
7
var ints = (int[]) (object) channels;
Zakharia Stanley
  • 1,156
  • 1
  • 9
  • 9
5

You can do a cast in the select clause:

var myInts = channels.Select(x => (int)x);
Matten
  • 17,025
  • 1
  • 40
  • 63
5

Another way to do the same, without LINQ:

var vals = Array.ConvertAll(channels, c =>(int)c);
Marc Gravell
  • 976,458
  • 251
  • 2,474
  • 2,830
2

var myInts = channels.Select(x=x.Value) this doesn't work because = needs to be => (int)x

I think var myInts = channels.Select(x => (int)x).ToArray(); will do what you want.

evanmcdonnal
  • 42,528
  • 15
  • 96
  • 110
1

This should do it:

public void DoIt(params MyEnum[] channels)
{
   int[] ret = channels.Select(x => ((int)x)).ToArray();
}
gideon
  • 19,121
  • 11
  • 72
  • 112
1

You could do:

var myInts = channels.Select(e => (int)e);

Or as a LINQ query:

var myInts = from e in channels select (int)e;

You could then call ToArray() or ToList() on myInts to get a MyEnum[] or a List<MyEnum>

Mike Christensen
  • 82,887
  • 49
  • 201
  • 310