I would declare an empty String variable like this:
string myString = string.Empty;
Is there an equivalent for a \'DateTime\' variable
Option 1: Use a nullable DateTime?
Option 2: Use DateTime.MinValue
Personally, I'd prefer option 1.
There's no such thing as an empty date per se, do you mean something like:
DateTime? myDateTime = null;
You may want to use a nullable datetime. Datetime? someDate = null;
You may find instances of people using DateTime.Max
or DateTime.Min
in such instances, but I highly doubt you want to do that. It leads to bugs with edge cases, code that's harder to read, etc.
Either:
DateTime dt = new DateTime();
or
DateTime dt = default(DateTime);
This will work for null able dateTime parameter
. .
SearchUsingDate(DateTime? StartDate, DateTime? EndDate){
DateTime LastDate;
if (EndDate != null)
{
LastDate = (DateTime)EndDate;
LastDate = LastDate.AddDays(1);
EndDate = LastDate;
}
}
No. You have 2 options:
DateTime date = DateTime.MinValue;
This works when you need to do something every X amount of time (since you will always be over MinValue
) but can actually cause subtle errors (such as using some operators w/o first checking if you are not MinValue
) if you are not careful.
And you can use Nullable
:
DateTime? date = null;
Which is nice and avoids most issues while introducing only 1 or 2.
It really depends on what you are trying to achieve.