C#
C#

help

Root Question Message

CrosRoad95
CrosRoad9511/12/2022
❔ 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))
Message Not Public

Sign In and Join Server To See

11/12/2022
Message Not Public

Sign In and Join Server To See

11/12/2022
plam
plam11/12/2022
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;
    }
  }
}
thinker227
thinker22711/12/2022
That's just a slower version of TryParse
plam
plam11/12/2022
ikr?
CrosRoad95
CrosRoad9511/12/2022
so i will stick with guid.tryparse 😄
Message Not Public

Sign In and Join Server To See

11/12/2022
Message Not Public

Sign In and Join Server To See

11/12/2022
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy