13

Please tell me how to validate GUID in .net and it is unique for always?

1

8 Answers 8

31

Guid's are unique 99.99999999999999999999999999999999% of the time.

It depends on what you mean by validate?

Code to determine that a Guid string is in fact a Guid, is as follows:

private static Regex isGuid = new Regex(@"^(\{){0,1}[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}(\}){0,1}$", RegexOptions.Compiled); internal static bool IsGuid(string candidate, out Guid output) { bool isValid = false; output = Guid.Empty; if(candidate != null) { if (isGuid.IsMatch(candidate)) { output=new Guid(candidate); isValid = true; } } return isValid; } 
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for trying not to use exceptions, -1 because a Guid can have other formats and the current method will throw an exception for the following case: "{230a0f8b-81fb-4052-866e-9ac6a7611c77"
+1, In .NET 4.0, we'll finally see the introduction of Guid.TryParse(), which would obviously be preferable to using a Regex. For now though, this is perfectly sufficient and acceptable. connect.microsoft.com/VisualStudio/feedback/details/94072/…
Guid's are not random, maybe there's a way to also check if they meet these criteria: blogs.msdn.com/b/oldnewthing/archive/2008/06/27/8659071.aspx
13

2^128 is a very, very large number. It is a billion times larger than the number of picoseconds in the life of the universe. Too large by a long shot to ever validate, the answer is doomed to be "42". Which is the point of using them: you don't have to. If you worry about getting duplicates then you worry for the wrong reason. The odds your machine will be destroyed by a meteor impact are considerably larger.

Duck!

Comments

2

Here's a non-Regex answer that should be pretty fast:

public static bool IsHex(this char c) { return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')); } public static bool IsGuid(this string s) { // Length of a proper GUID, without any surrounding braces. const int len_without_braces = 36; // Delimiter for GUID data parts. const char delim = '-'; // Delimiter positions. const int d_0 = 8; const int d_1 = 13; const int d_2 = 18; const int d_3 = 23; // Before Delimiter positions. const int bd_0 = 7; const int bd_1 = 12; const int bd_2 = 17; const int bd_3 = 22; if (s == null) return false; if (s.Length != len_without_braces) return false; if (s[d_0] != delim || s[d_1] != delim || s[d_2] != delim || s[d_3] != delim) return false; for (int i = 0; i < s.Length; i = i + (i == bd_0 || i == bd_1 || i == bd_2 || i == bd_3 ? 2 : 1)) { if (!IsHex(s[i])) return false; } return true; } 

Comments

1

You cannot validate GUID's uniqueness. You just hope it was generated with a tool that produces unique 16 bytes. As for validation, this simple code might work (assuming you are dealing with GUID's string representation:

bool ValidateGuid(string theGuid) { try { Guid aG = new Guid(theGuid); } catch { return false; } return true; } 

3 Comments

Thats the easy way out. Remember that Exceptions should be used for exceptional circumstances, not for validating types.
@Kyle Rozendo: Yeah, but with Regex, you pretty much limit the format of GUID's string representation. For example, in your code, the formatting is WITH DASHES only.
Yeah, we would never want to take "the easy way out" when writing tons of code to do the same thing is so much better... What if this was a unit test? Would it then be acceptable?
0

this question was already discussed in this post. You may find more interesting details

Comments

0

In .net 4, you can use this extension method

public static class GuidExtensions { public static bool IsGuid(this string value) { Guid output; return Guid.TryParse(value, out output); } } 

Comments

0

i wrote a extension for this

public static bool TryParseGuid(this Guid? guidString) { if (guidString != null && guidString != Guid.Empty) { if (Guid.TryParse(guidString.ToString(), out _)) { return true; } else return false; } return false; } 

1 Comment

Method name TryParseGuid would usually suggest that parsing output is returned as out parameter (like Enum.TryParse etc.). Think of renaming this extension method to IsGuidValid or similar, which will clearly indicate that it returns bool
0

If you're looking for a way to determine if it's the format of the actual .Net Guid type, take a look at this article. A quick regex does the trick.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.