Is there a GUID.TryParse() in .NET 3.5? Is there a GUID.TryParse() in .NET 3.5? asp.net asp.net

Is there a GUID.TryParse() in .NET 3.5?


There is no Guid.TryParse in CLR 2.0 and earlier. It will be available starting with CLR 4.0 and Visual Studio 2010.

As to why there wasn't. These types of questions are usually hard to answer correctly. Most likely it was an oversight or a time constraint issue. If you open up mscorlib in reflector you'll see there is actually a method named TryParse on Guid but it's private. It also throws an exception in certain cases so it's not a good equivalent to say Int32.TryParse.


Guid.TryParse implementation using regular expressions.


IsGuid implemented as extension method for string...

public static bool IsGuid(this string stringValue){   string guidPattern = @"[a-fA-F0-9]{8}(\-[a-fA-F0-9]{4}){3}\-[a-fA-F0-9]{12}";   if(string.IsNullOrEmpty(stringValue))     return false;   Regex guidRegEx = new Regex(guidPattern);   return guidRegEx.IsMatch(stringValue);}