Please tell me how to validate GUID in .net and it is unique for always?
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; } 3 Comments
Guid can have other formats and the current method will throw an exception for the following case: "{230a0f8b-81fb-4052-866e-9ac6a7611c77"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/…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
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
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
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
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 boolIf 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.