Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 83329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:45:23+00:00 2026-05-10T21:45:23+00:00

I want to try to convert a string to a Guid, but I don’t

  • 0

I want to try to convert a string to a Guid, but I don’t want to rely on catching exceptions (

  • for performance reasons – exceptions are expensive
  • for usability reasons – the debugger pops up
  • for design reasons – the expected is not exceptional

In other words the code:

public static Boolean TryStrToGuid(String s, out Guid value) {     try     {         value = new Guid(s);         return true;     }     catch (FormatException)     {         value = Guid.Empty;         return false;     } } 

is not suitable.

I would try using RegEx, but since the guid can be parenthesis wrapped, brace wrapped, none wrapped, makes it hard.

Additionally, I thought certain Guid values are invalid(?)


Update 1

ChristianK had a good idea to catch only FormatException, rather than all. Changed the question’s code sample to include suggestion.


Update 2

Why worry about thrown exceptions? Am I really expecting invalid GUIDs all that often?

The answer is yes. That is why I am using TryStrToGuid – I am expecting bad data.

Example 1 Namespace extensions can be specified by appending a GUID to a folder name. I might be parsing folder names, checking to see if the text after the final . is a GUID.

c:\Program Files c:\Program Files.old c:\Users c:\Users.old c:\UserManager.{CE7F5AA5-6832-43FE-BAE1-80D14CD8F666} c:\Windows c:\Windows.old 

Example 2 I might be running a heavily used web-server wants to check the validity of some posted back data. I don’t want invalid data tying up resources 2-3 orders of magnitude higher than it needs to be.

Example 3 I might be parsing a search expression entered by a user.

enter image description here

If they enter GUID’s I want to process them specially (such as specifically searching for that object, or highlight and format that specific search term in the response text.)


Update 3 – Performance benchmarks

Test converting 10,000 good Guids, and 10,000 bad Guids.

Catch FormatException:    10,000 good:     63,668 ticks    10,000 bad:   6,435,609 ticks  Regex Pre-Screen with try-catch:    10,000 good:    637,633 ticks    10,000 bad:     717,894 ticks  COM Interop CLSIDFromString    10,000 good:    126,120 ticks    10,000 bad:      23,134 ticks 

p.s. I shouldn’t have to justify a question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:45:24+00:00Added an answer on May 10, 2026 at 9:45 pm

    Performance Benchmarks

    Catch exception:    10,000 good:    63,668 ticks    10,000 bad:  6,435,609 ticks  Regex Pre-Screen:    10,000 good:   637,633 ticks    10,000 bad:    717,894 ticks  COM Interop CLSIDFromString    10,000 good:   126,120 ticks    10,000 bad:     23,134 ticks 

    COM Intertop (Fastest) Answer:

    /// <summary> /// Attempts to convert a string to a guid. /// </summary> /// <param name='s'>The string to try to convert</param> /// <param name='value'>Upon return will contain the Guid</param> /// <returns>Returns true if successful, otherwise false</returns> public static Boolean TryStrToGuid(String s, out Guid value) {    //ClsidFromString returns the empty guid for null strings       if ((s == null) || (s == ''))       {             value = Guid.Empty;             return false;       }     int hresult = PInvoke.ObjBase.CLSIDFromString(s, out value);    if (hresult >= 0)    {       return true;    }    else    {       value = Guid.Empty;       return false;    } }   namespace PInvoke {     class ObjBase     {         /// <summary>         /// This function converts a string generated by the StringFromCLSID function back into the original class identifier.         /// </summary>         /// <param name='sz'>String that represents the class identifier</param>         /// <param name='clsid'>On return will contain the class identifier</param>         /// <returns>         /// Positive or zero if class identifier was obtained successfully         /// Negative if the call failed         /// </returns>         [DllImport('ole32.dll', CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = true)]         public static extern int CLSIDFromString(string sz, out Guid clsid);     } } 

    Bottom line: If you need to check if a string is a guid, and you care about performance, use COM Interop.

    If you need to convert a guid in String representation to a Guid, use

    new Guid(someString); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer What legal steps should you take to protect your products… May 11, 2026 at 3:20 pm
  • added an answer You may use Jetty or some other embeddable HTTP server.… May 11, 2026 at 3:20 pm
  • added an answer There is so much misinformation in the answers in this… May 11, 2026 at 3:20 pm

Related Questions

I want to try to convert a string to a Guid, but I don't
In C# I try to send a string through TcpClient as such: byte[] outputOutStream
I am faced with the need to pull out the information in a string
I have a string, say '123' , and I want to convert it to
I am working with an open-source UNIX tool that is implemented in C++, and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.