104

If DateTime is an object and default C# parameters can only be assigned compile-time constants, how do you provide default values for objects like DateTime?

I am trying to initialize values in a POCO with a constructor, using named parameters with default values.

1

6 Answers 6

191

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(); 
Sign up to request clarification or add additional context in comments.

6 Comments

What about DateTime.MinValue?
@Dr.Zim: No it's readonly but not constant.
And why wouldn't DateTime.MinValue be a compiler-time constant?
@Dr. Zim: because DateTime can't be declared as const. I don't know why that's the way it is.
you can use: DateTime dt = default(DatetTime)
|
60

The only way you can do this directly is to use the value default(DateTime), which is compile-time constant. Or you can work around that by using DateTime? and setting the default value to null.

See also this related question about TimeSpan.

2 Comments

Just what I was looking for. Sometimes using DateTime? is messier.
thanks.. default or minvalue by @Dr. Zim does the trick for me. but default is what i like. saved me from posting another question! :)
8

new DateTime() also equals DateTime.MinValue

You could a create a default parameter like so.

void test(DateTime dt = new DateTime()) { //... } 

4 Comments

Doesn't work, you cannot use functions in a default parameter.
@Kirbinator it works try it.
I can't believe it compiles when I do this. I have yet to see, though, if it will be fine at runtime.
Since it is DateTime is struct it will work.
4

Unlike VB, C# doesn't support date literals. And since optional parameters look like this in IL, you can't fake it with attributes.

.method private hidebysig static void foo([opt] int32 x) cil managed { .param [1] = int32(5) .maxstack 8 L_0000: nop L_0001: ret } .method //this is a new method private hidebysig static //it is private, ???, and static void foo //it returns nothing (void) and is named Foo ([opt] int32 x) //it has one parameter, which is optional, of type int32 .param [1] = int32(5) //give the first param a default value of 5 

2 Comments

Can you please explain this code?
ok thank you :) Date time also we can give the same manner like .param [1] = DateTime(5) ?
-1
private System.String _Date= "01/01/1900"; public virtual System.String Date { get { return _Date; } set { _Date= value; } } 

We can assign value to a label like given below,

lblDate.Text = Date; 

Also we can get the value,

DateTime dt = Convert.ToDateTime(label1.Text); 

1 Comment

Cannot be used as parameter still. Does not answer the question.
-3

you could use:

Datetime.MinValue 

for initialization.

1 Comment

Doesn't answer the question as you can't set this as the default value as it's not a constant

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.