Why following program is not executing?
using System;
class Program
{
static void Main()
{
int[] intA = {1,2,3,4,5};
string[] stringA = {"aa","bb","cc" };
A(intA);
A(stringA);
Console.ReadKey();
}
static void A(object[] x)
{
foreach (object item in x)
{
Console.WriteLine(item);
}
}
}
Loading
Posted Sep 6, 2013, 7:42 PM
VulpesPosted Sep 6, 2013, 7:28 PM
It's documented on MSDN here:
http://msdn.microsoft.com/en-us/library/bb341406.aspx
However, unless you're wanting to learn the whole of LINQ at this point in your studies, you might be better sticking to 'traditional' code for the time being:
Posted Sep 6, 2013, 5:52 AM
I wish to know whether online resources available to learn this (object[] k = intA.Cast
VulpesPosted Sep 6, 2013, 5:42 AM
using System.Linq;
and it will compile and work fine.
Posted Sep 6, 2013, 5:33 AM
using System;
class Program
{
static void Main()
{
int[] intA = { 1, 2, 3, 4, 5 };
string[] stringA = { "aa", "bb", "cc" };
object[] k = intA.Cast
Jignesh TrivediPosted Sep 6, 2013, 3:00 AM
Agree with Vulpes.
you have to convert you int array to object array...
try...
int[] intA = { 1, 2, 3, 4, 5 };
object[] k = intA.Cast
Posted Sep 5, 2013, 4:11 PM
VulpesPosted Sep 5, 2013, 3:20 PM
If you were to comment out this line:
A(intA);
then you'd find that the program does compile and produce the expected output to the console:
aa
bb
cc
This is because there is a conversion from string[] to object[] as a result of something called 'array covariance'. However, this only applies to arrays of reference types - it doesn't apply to arrays of value types such as int[].
For more on array covariance, check out this link:
http://msdn.microsoft.com/en-us/library/aa664572(v=vs.71).aspx