Based on the accepted answer, I created an extension method as follows:
public static Guid ToGuid(this string aString) { Guid newGuid; if (string.IsNullOrWhiteSpace(aString)) { return MagicNumbers.defaultGuid; } if (Guid.TryParse(aString, out newGuid)) { return newGuid; } return MagicNumbers.defaultGuid; }
Where "MagicNumbers.defaultGuid" is just "empty" all zero Guid "00000000-0000-0000-0000-000000000000".
In my case, returning this value as a result of an incorrect ToGuid conversion was not a problem.
a4bike Mar 19 '16 at 18:42 2016-03-19 18:42
source share