Optional non-system type parameters
C#4.0 brings optional parameters, which I've been waiting for for quite some time. However it seems that because only System types can be const
, I cannot use any class/struct which I have created as an optional parameter.
Is there a some way which allows me to use a more complex t开发者_JS百科ype as an optional parameter. Or is this one of the realities that one must just live with?
The best I could come up with for reference types was:
using System;
public class Gizmo
{
public int Foo { set; get; }
public double Bar { set; get; }
public Gizmo(int f, double b)
{
Foo = f;
Bar = b;
}
}
class Demo
{
static void ShowGizmo(Gizmo g = null)
{
Gizmo gg = g ?? new Gizmo(12, 34.56);
Console.WriteLine("Gizmo: Foo = {0}; Bar = {1}", gg.Foo, gg.Bar);
}
public static void Main()
{
ShowGizmo();
ShowGizmo(new Gizmo(7, 8.90));
}
}
You can use the same idea for structs by making the parameter nullable:
public struct Whatsit
{
public int Foo { set; get; }
public double Bar { set; get; }
public Whatsit(int f, double b) : this()
{
Foo = f; Bar = b;
}
}
static void ShowWhatsit(Whatsit? s = null)
{
Whatsit ss = s ?? new Whatsit(1, 2.3);
Console.WriteLine("Whatsit: Foo = {0}; Bar = {1}",
ss.Foo, ss.Bar);
}
You can use any type as an optional parameter:
using System;
class Bar { }
class Program
{
static void Main()
{
foo();
}
static void foo(Bar bar = null) { }
}
Okay, I reread your question and I think I see what you mean - you want to be able to do something like this:
static void foo(Bar bar = new Bar()) { }
Unfortunately this is a not allowed since the value of the default parameter must be known at compile time so that the compiler can bake it into the assembly.
精彩评论