Parse
int.Parse("12.44");
Exception : Above code will generate the exception Input string was not in a correct format, just because you are trying to cast float / real value in Integer. Only it is parsing if your parameter value must match the desire type in which you are trying to parse.
TryParse
case #1
int.TryParse("14.55",out a);
case #2
int.TryParse("14", out a);
Output : Above code (case 1) will return false, just because parsing operation gets failed and value for variable 'a' would be 'zero'. Here consider 'a' is output type of parameter like we have in Oracle / SQL Server (not exactly same but behavior is same). If parsing get succeeded than it will return true and target variable (i.e. 'a') will have the parsed value, like in code (case 2), 'a' will return 14.
Difference between out and ref type
ref : parameter variable need to be assigned before passing to called function otherwise you will get the compile time error/there is no compulsion for called function to assign value in ref type parameter variable.
out : parameter variable need not to be assigned before passing in the called function but here we have compulsion for called method to assign a value in out type parameter variable before control leaves the current method as compile time error comes.
ParseExact
Case #1
string dateString = "Mon 16 Jun 8:30 AM 2008";
System.DateTime dt = DateTime.Parse(dateString).ToString("dd-MMM-yyyy")
MSDN : Because the Parse (String) method tries to parse the string representation of a date and time using the formatting rules of the current culture, trying to parse a particular string across different cultures can either fail or return different results If a specific date and time format will be parsed across different locales, use the DateTime.Parse(String, IFormatProvider) method or one of the overloads of the ParseExact method and provide a format specifier.
Exception : when you try to run this code it will give you an exception "String was not recognized as a valid DateTime".
Case #2
string dateString = "Mon 16 Jun 8:30 AM 2008";
string dateformat = "ddd dd MMM h:mm tt yyyy";
Response.Write(DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture).ToString("dd-MMM-yyyy"));
Output : The CultureInfo.InvariantCulture property is used if you are formatting or parsing a string that should be parse able by a piece of software independent of the user's local settings.
TryParseExact
Case #1
string dateString = "Mon 16 Jun 8:30 AM 2008";
string dateformat = "ddd dd MMM h:mm tt yyyy";
DateTime dt;
if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
Response.Write("After Parsed : " + dt.ToString("dd-MMM-yyyy"));
else
Response.Write("Parsing Failed:" + dt.ToString("dd-MMM-yyyy"));
Output : Would be "After Parsed : " + , because your target date is exactly matched the format that you mentioned.
Case #2:
string dateString = "Mon 16 Jun 8:30 AM 2008";
string dateformat = "dd ddd MMM h:mm tt yyyy";
if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
Response.Write("After Parsed : " + dt.ToString("dd-MMM-yyyy"));
else
Response.Write("Parsing Failed:" + dt.ToString("dd-MMM-yyyy"));
Output : Would be "Parsing Failed : 01-Jan-0001", because your target date is not exactly matched as the format that you mentioned.