views:

97

answers:

2

C#4.0 obviously brings optional parameters (which I've been waiting for, for quite some time). However it seems that because only system types can be const that I cannot use any class/struct which I have created, as an optional param.

Is there a some way which allows me to use a more complex type as an optional parameter. Or is this one of the realities that one must just live with?

+5  A: 

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.

Andrew Hare
+2  A: 

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);
}
Niall C.
I was thinking along those lines but as I was using a struct it didn't like that either. Don't know why I didn't just make the func take a nullable version (Size? size = null) though.
Courtney de Lautour
System.Nullable... Now I remember where I got the idea for an extra "initialized" field in the struct. :)
Niall C.
FYI, you can still use the ?? operator with a nullable struct.
Courtney de Lautour
@Courtney - you're right. Edited again.
Niall C.