C
C#2y 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 User2y ago
Message Not Public
Sign In & Join Server To View
plam
plam2y 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
Thinker2y ago
That's just a slower version of TryParse
plam
plam2y ago
ikr?
CrosRoad95
CrosRoad952y ago
so i will stick with guid.tryparse 😄
Unknown User
Unknown User2y 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
More Posts
❔ .NET CORE 6 servicesSo i want add a service "AddContentful" but idk how to fix this because i can only find configuratio❔ Console Game Project (Memory Matching)Hi all! I'm not sure if this is the right channel, but I have a question... I'm New to programming (Find all HREFs out of string and extract valueHey, I am currently developing a web scraper and therefore want to extract all available information❔ How to store session data backend?I'm building a similar game to skribbl.io which is a drawing game where you draw with other people. ❔ How do i sort a list of objects by multiple possible itemsHey, i'm trying to write a method to return a list of objects by letting the user choose which item ❔ ASP.NET Core how to let a controller be mapped only in development modeThis is pretty easy to do with minimal API's, you just do a check and map only if it succeeds: ```cs❔ Turn off VS 2022 PreviewHow do I turn off VS 2022 Preview? It is showing on the splash screen and everywhere. I turned it on❔ Winforms rotated text in `ToolStripButton` appears boldWhen setting the direction of text to e.g. Vertical270, the text in the button becomes different, eiIf I do console.clear() it just skips a bunch of lines instead of clearingI am using vsc c# and I think its an error with .net because I saw a few forums talking about it, pl❔ Why wont visual studio auto format this with CTRL K + CTRL D?can anyone think of why visual studio wont format these lambda arrows with `CTRL K + CTRL D` but it