Can i override Convert.ToDateTime()?
c#, datetime
Solution
No, you can't override static methods. But you can write your own static method:
// TODO: Think of a better class name - this one sucks :)
public static class MoreConvert
{
public static DateTime? ToDateTimeOrNull(string text)
{
return text == null ? (DateTime?) null : Convert.ToDateTime(text);
}
}
Note that the return type has to be `DateTime?` because `DateTime` itself is a non-nullable value type.
You might also want to consider using `DateTime.ParseExact` instead of `Convert.ToDateTime` - I've never been terribly fond of its lenient, current-culture-specific behaviour. It depends where the data is coming from though. Do you know the format? Is it going to be in the user's culture, or the invariant culture? (Basically, is it user-entered text, or some machine-generated format?)
Problem
can i override `Convert.ToDateTime()`? I don't want 100 times or more check if string is nul and if is not then convert it to DateTime. Can i override this function to check if is null then will return null otherway convert it.