I've been trying to make this simple method work:
public static object[] IsVow(object[] a)
{
return a.Select(x =>
{
char test = (char)x;
return "aeiou".Contains(test) ? test : x;
}).ToArray();
}
Where a is an object[] of ints (like { 97, 99, 101 }, where it should return { 'a', 99, 'e' }).
However, it fails at char test = (char)x, printing System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Char', even though replacing (char)x with something like (char)97 works fine.
I coded two test methods to figure out what the problem was, and the fact that it's an object array seems to break it:
public static object[] objtest(object[] a)
{
return a.Select(x =>
{
char test = (char)x;
Console.WriteLine(test);
return (object)(int)test;
}).ToArray();
}
public static int[] inttest(int[] a)
{
return a.Select(x =>
{
char test = (char)x;
Console.WriteLine(test);
return (int)test;
}).ToArray();
}
Using the same array, inttest() works perfectly, but objtest() breaks at the same spot, even though they basically do the same thing. I'm not sure what causes this, I was under the impression that object allowed you to store any value of any type within it.