Go to home page mail me! RSS Feed
FoxMetrics Web Analytics

TryParse on nullable types

Saturday, March 13, 2010 3:37 PM

For some unknown reason you can’t do a TryParse on nullable types or should I say the out parameter of a TryParse can not be nullable? For example this just won’t work.

DateTime? dt;
DateTime.TryParse("12/12/2010", out dt);
Error 4 The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments

The solution or workaround is to use two variables to accomplish the goal, here is an example.

DateTime? dt1;
DateTime dt2;
dt1 = DateTime.TryParse("12/12/2010", out dt2) ? dt2 : (DateTime?)null;
DotNetKicks Image

Your Comments.

No comments posted yet.

Your Reply.

Comment Form.

Fields denoted with a "*" are required.

You may also like to leave your email or website.

 
Please add 5 and 5 and type the answer here:

Preview Your Comment.

 
Next entries »