C# 4 default parameter values: How to assign a default DateTime/object value? [duplicate]

DateTime cannot be used as a constant but you could make it a nullable type (DateTime?) instead.

Give the DateTime? a default value of null, and if it is set to null at the start of your function, then you can initialize it to any value you want.

static void test(DateTime? dt = null)
{
    if (dt == null)
    {
        dt = new DateTime(1981, 03, 01);
    }

    //...
}

You can call it with a named parameter like this:

test(dt: new DateTime(2010, 03, 01));

And with the default parameter like this:

test();

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)