This is a followup on a thread I thought was resolved yesterday. Yesterday I was having problems with my code in the following case:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
class Bar
{
int v;
public Bar(int v) { this.v = v; }
public override string ToString() { return v.ToString(); }
}
static void Main(string[] args)
{
Foo(1, 2, 3);
Foo(new int[] { 1, 2, 3 });
Foo(new Bar(1), new Bar(2), new Bar(3));
Foo(new Bar[] { new Bar(1), new Bar(2), new Bar(3) });
System.Threading.Thread.Sleep(20000);
}
static void Foo(params object[] objs)
{
Console.WriteLine("New call to Foo: ");
foreach(object o in objs)
Console.WriteLine("Type = " + o.GetType() + ", value = "+o.ToString());
}
}
}
If you run this you can see a problem with the last call to Foo. The fact that the argument is a vector is “lost”.
So…. anyone know how to report a C# compiler bug? Or would this be considered a reflection bug?
(What a relief: I was bummed to think I had wasted time here with a bug of my own. In fact it is a C# bug after all, and I’m vindicated! And how often do we get to see actual C# compiler bugs these days? Not common…)
I would expect these two calls to function identically- a params argument is an array in the called method. Jon Skeet’s example in the previous question works because an array of int’s is not covariant to an array of objects (and so is treated as
new Object[] { new Int[] {1,2,3} }), but in this example an array of FooBars is covariant to an array of objects, and so your parameter is expanded into theobjsargument.Wikipedia of all things covers this exact case: Covariance and contravariance (computer science)
Sorry but I am sure that this is not a compiler bug.
EDIT:
You can achieve what you want thus:
NEW EDIT (other author):
Or simply use: