views:

1233

answers:

7
+10  Q: 

GUID.TryParse() ?

Obviously there is no public GUID.TryParse() in .NET CLR 2.0.

So, I was looking into regular expressions [aka googling around to find one] and each time I found one there was a heated argument in the comments section about RegEx A doesn't work, use RegEx B. Then someone would write Regex C yadda yadda

So anyway, What I decided to do was this, but I feel bad about it.

public static bool IsGuid (string possibleGuid) {

    try {
      Guid gid = new Guid(possibleGuid);
      return true;    
    } catch (Exception ex) {
      return false;
    }
}

Obviously I don't really like this since it's been drilled into me since day one to avoid throwing exceptions if you can defensibly code around it.

Does anyone know why there is no public Guid.TryParse() in the .NET Framework?

Does anyone have a real Regular Expression that will work for all GUIDs?

EDIT ----

Guid.TryParse is available in .NET 4.0

+3  A: 

In terms of why there isn't one, it's an oversight. There will be a Guid.TryParse in .NET 4 (see BCL blog post for details).

bdukes
+18  A: 

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.

JaredPar
+1 simply because it is an interesting answer. The TryParse I noticed is called by the constructor.
RichardOD
Very interesting. Looking at the Guid(string) constructor implementation in reflector made my head spin. I wonder how the guys at Redmond maintain this code :-)
Darin Dimitrov
Yeah that's why I mentioned "why wasnt there a Public TryParse()" Thanks jared
Jack Marchetti
+5  A: 

Guid.TryParse implementation using regular expressions.

Darin Dimitrov
Nice. I'd probably add in a try catch fallback in there too, just to be sure (at least until I was confident I hadn't missed anything in the Regex).
RichardOD
Have you implemented this?
Jack Marchetti
No, so far in my programming carrier I've never needed such functionality.
Darin Dimitrov
+1  A: 

There's no TryParse functionality in the .NET Framework to my knowledge at this moment. You'll have to resort to RegEx or the try-catch option. RegEx isn't my cup of tea, so I'm sure someone else will post an answer.

Exceptions are expensive performance wise, so my vote goes to the RegEx option.

Yannick Compernol
A: 

This should work:

@"^\{?[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{12}\}?$"
jheddings
That says that "{00000000-0000-0000-0000-000000000000" is a legal GUID. Do you *really* want to allow mismatched braces? And why are the hyphens optional?
Eric Lippert
This was intended to be a "quick and dirty" way to match GUID strings. To do the appropriate brace-matching and hyphen usage would be very convoluted in a regex (it would really be several expressions). According to the docs for `Guid(string)`, the hyphens are optional.
jheddings
A: 

You can write your own TryParse as a extension method for Guid. Then when the 'real' one from MS shows up, your already good to go and don't have to change.

ryber
Except that the extension method will only apply to a `Guid` instance, and the framework version will be static.
LukeH
Ah yes, dag nabbit
ryber
+1  A: 
Partha Choudhury