Cast generic type parameter into array
c#, c#-4.0
Solution
Try something like this:
public void Print()
{
var array = Value as Array;
if (array != null)
foreach (var item in array)
Console.WriteLine(item);
}
The as keyword:
The as operator is like a cast operation. However, if the conversion isn't possible, as returns null instead of raising an exception.
Problem
If I know that a certain generic type parameter is an array, how do I convert it into an array or an `IEnumerable` so I can see its items? For e.g. ``` public class Foo<T> { public T Value { get; set; } public void Print() { if (Value.GetType().IsArray) foreach (var item in Value /*How do I cast this to Array or IEnumerable*/) Console.WriteLine(item); } } ```