Please tell me how to validate GUID in .net and it is unique for always?
-
1Guid's are always unique if all the generators play by the rules: http://blogs.msdn.com/b/oldnewthing/archive/2008/06/27/8659071.aspx – Daniel Little May 18 '14 at 10:28
8 Answers
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;
}

- 21,988
- 13
- 81
- 109

- 25,001
- 7
- 80
- 118
-
+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" – João Angelo Mar 03 '10 at 11:43
-
6+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. http://connect.microsoft.com/VisualStudio/feedback/details/94072/guid-tryparse – Wim Mar 03 '10 at 11:43
-
Guid's are not random, maybe there's a way to also check if they meet these criteria: http://blogs.msdn.com/b/oldnewthing/archive/2008/06/27/8659071.aspx – Daniel Little May 18 '14 at 10:27
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!

- 922,412
- 146
- 1,693
- 2,536
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;
}

- 5,370
- 7
- 50
- 81
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;
}

- 2,930
- 2
- 21
- 34
-
1Thats the easy way out. Remember that Exceptions should be used for exceptional circumstances, not for validating types. – Kyle Rosendo Mar 03 '10 at 11:26
-
3@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. – Kerido Mar 03 '10 at 11:33
-
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? – iGanja Nov 05 '13 at 00:16
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.

- 21,988
- 13
- 81
- 109

- 623,446
- 136
- 1,297
- 1,155
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);
}
}

- 6,895
- 2
- 31
- 43
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;
}

- 305
- 1
- 4
- 18
-
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` – rsobon Apr 02 '20 at 07:07