C
C#3y ago
CrosRoad95

❔ check if string is valid guid without guid.TryParse

as in title, i would like to avoid conditions like if (!Guid.TryParse(string, out var _)), instead i want something like if (!Guid.IsValid(string))
7 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
plam
plam3y ago
TryParse would be great here, but as you've mentioned to avoid it, here's what you can actually do:
public static class Example {
public static bool TryParseGuid(string guidString, out Guid guid)
{
if (guidString == null) throw new ArgumentNullException("guidString");
try
{
guid = new Guid(guidString);
return true;
}
catch (FormatException)
{
guid = default(Guid);
return false;
}
}
}
public static class Example {
public static bool TryParseGuid(string guidString, out Guid guid)
{
if (guidString == null) throw new ArgumentNullException("guidString");
try
{
guid = new Guid(guidString);
return true;
}
catch (FormatException)
{
guid = default(Guid);
return false;
}
}
}
Thinker
Thinker3y ago
That's just a slower version of TryParse
plam
plam3y ago
ikr?
CrosRoad95
CrosRoad95OP3y ago
so i will stick with guid.tryparse 😄
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server