How can I set default value for DateTime in optional parameter?
public SomeClassInit(Guid docId, DateTime addedOn = DateTime.Now???)
{
//Init codes here
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is a workaround for this, taking advantage of nullable types and the fact that
nullis a compile-time constant. (It’s a bit of a hack though, and I’d suggest avoiding it unless you really can’t.)In general, I’d prefer the standard overloading approach suggested in the other answers: