Skip to content

Commit 072b53b

Browse files
committed
Feat: ToGuidOrDefault & TryToGuid
1 parent 0cbfa81 commit 072b53b

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/CodeOfChaos.Extensions/StringExtensions.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,19 @@ public static bool IsNotNullOrWhiteSpace([NotNullWhen(true)] this string? str)
2929
public static string Truncate(this string input, int maxLength)
3030
=> input.Length <= maxLength ? input : input[..maxLength];
3131

32-
public static Guid ToGuid(this string input) {
33-
#if DEBUG
34-
if (Guid.TryParse(input, out Guid output)) return output;
32+
33+
// Because "testing" of the value is handled by analyzer, we can just "blindly" parse during release.
34+
public static Guid ToGuid(this string input)
35+
=> Guid.Parse(input);
3536

36-
Debug.Fail("Failed to parse Guid");
37+
public static Guid ToGuidOrDefault(this string input) {
38+
if (Guid.TryParse(input, out Guid guid)) return guid;
3739
return Guid.Empty;
38-
#else
39-
// Because "testing" of the value is handled by analyzer, we can just "blindly" parse during release.
40-
return Guid.Parse(input);
41-
#endif
4240
}
4341

42+
public static bool TryToGuid(this string input, out Guid guid)
43+
=> Guid.TryParse(input, out guid);
44+
4445
/// <summary>
4546
/// Converts a given string into a GUID by hashing it using the SHA256 algorithm.
4647
/// </summary>

0 commit comments

Comments
 (0)