Reputation: 4013
Please Help!
What am I doing wrong?
static void f1(Color color, params float[] f)
{
System.Console.WriteLine("Here is f1 for float");
}
static void f1(Color color, params int[] f)
{
System.Console.WriteLine("Here is f1 for int");
}
static void Main()
{
f1(null,0);
}
I can't invoke f1(null,0);
I get compile time error.
How this staff can be overcome assuming I indeed need those method signatures?
EDIT: As for Compile-tme error - ReSharper complains:
Cannot resolve method f1(null,int), candidates are:
void f1(Syste.Drawing.Color, params[] float)
void f1(Syste.Drawing.Color, params[] int)
Upvotes: 2
Views: 2276
Reputation: 2337
First of all, you cant use null for the Color parameter. To use one of both codes use this for ints:
f1(SystemColors.ActiveBorder, new int[]{0});
or this for floats:
f1(SystemColors.ActiveBorder, new float[]{0});
the way you use it it will always call the int version.
Upvotes: 0
Reputation: 38230
I think the problem is you are passing null for Color which might be upsetting the function, either change that to Color?
(since it is a struct) or pass a valid Color value
static void f1(Color? color, params float[] f)
static void f1(Color? color, params int[] f)
Upvotes: 4
Reputation: 11309
You need to pass System.Drawing.Color Object not null in Color parameter.
Upvotes: 0
Reputation: 21377
If Color is System.Drawing.Color
then parameter color
can't be null. If you want color
to be nullable use Color?
type
f1(Color.Black, 0) // works
f1(null, 0) // Doesn't work
Upvotes: 1
Reputation: 7326
You cannot pass null in place of a struct parameter, that is Color in your example. The problem is not about the params parameters. Some valid calls would be like;
f1(Color.Black, 0); // prints "Here is f1 for int"
f1(Color.Black, 0f); //prints "Here is f1 for float"
f1(Color.Black, 0, 5, 6, 7); // prints "Here is f1 for int"
f1(Color.Black, 0, 5.4f, 6, 7); //prints "Here is f1 for float"
Upvotes: 3