Ever needed to see if a string contained a date or int? In .NET 1.1, about the only way to do this is to use DateTime.Parse() and see if it throws an exception. That's a bad time.
Now thanks to TryParse(), you can check and see if the input is valid, before parsing. This prevents the need for using a try/catch block every time you use the parse method.
The syntax is kind of wierd so this method can be called in two ways:
Method 1:
string dateTimeString = "12/23/2004 09:00 AM";
DateTime myDateTime;
// returns true if dateTimeString is valid
if (DateTime.TryParse(dateTimeString, myDateTime))
myDateTime = DateTime.Parse(dateTimeString);
You can also get the DateTime directly from an output parameter.
Method 2:
string dateTimeString = "12/23/2004 09:00 AM";
DateTime myDateTime;
DateTime.TryParse(dateTimeString, out myDateTime);
It can also be used for ints, bytes, chars, doubles, bools, etc.
i.e.:
int32.TryParse(intString, myInt);
bool.TryParse(boolString, myBool);
It's a simple feature, but I think it will prove to be useful in the future.
More information can be found here:
ms-help://MS.VSCC.v80/MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/cpref/html/M_System_DateTime_TryParse_2_c11ce641.htm
Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=225