I use c# asp.net 4.
I need to create a Guid from a String. The String could be in the right format (so a Guid can be created) or in a not accepted format – In this case I need to set a variable isGuid to false.
At the moment I use this approach. As you can see I’m driven logic using a Try Catch.
I would like to know if you know a better way to perform this operation, to be honest I’m not sure if the use or Try Catch is appropriate here.
PS: If you feel the Title of my Q is not appropriate please let me know I will change it. Thanks!
PS2: If you know a better form of syntax, let me know I’m pretty new at coding.
string filename;
bool isGuid;
Guid guid;
try
{
guid = new Guid(filename);
isGuid = true;
}
catch
{
isGuid = false;
}
if(isGuid)
// Do smt here!
If you’re using .NET 4, you can use
Guid.TryParse: