97

What is the difference between these two ways of converting a string to System.Guid? Is there a reason to choose one over the other?

var myguid = Guid.Parse("9546482E-887A-4CAB-A403-AD9C326FFDA5");

or

var myguid = new Guid("9546482E-887A-4CAB-A403-AD9C326FFDA5");
Boann
  • 48,794
  • 16
  • 117
  • 146
brennazoon
  • 1,379
  • 2
  • 12
  • 11

4 Answers4

105

A quick look in the Reflector reveals that both are pretty much equivalent.

public Guid(string g)
{
    if (g == null)
    {
       throw new ArgumentNullException("g");
    }
    this = Empty;
    GuidResult result = new GuidResult();
    result.Init(GuidParseThrowStyle.All);
    if (!TryParseGuid(g, GuidStyles.Any, ref result))
    {
        throw result.GetGuidParseException();
    }
    this = result.parsedGuid;
}

public static Guid Parse(string input)
{
    if (input == null)
    {
        throw new ArgumentNullException("input");
    }
    GuidResult result = new GuidResult();
    result.Init(GuidParseThrowStyle.AllButOverflow);
    if (!TryParseGuid(input, GuidStyles.Any, ref result))
    {
        throw result.GetGuidParseException();
    }
    return result.parsedGuid;
}
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • Thanks for the response. I was really looking for "is their a difference in how they work". – brennazoon Aug 02 '11 at 18:58
  • 1
    Looks like there's a clear difference in the GuidParseThrowStyle used, so one could throw an error for input that the other will not. – Triynko Feb 13 '17 at 17:12
  • 5
    @Triynko: If you look at the code, you will see that they both throw for the same causes. The only difference is that if an `OverflowException` is thrown it will be encapsulated in a `FormatException` in case of `Guid.Parse`. – Luca Cremonesi Oct 26 '17 at 16:25
33

Use the version that is the most readable to you. The two are implemented almost exactly the same way.

The only real difference is that the constructor initializes itself to Guid.Empty before attempting the parse. However, the effective code is identical.

That being said, if the Guid is coming from user input, then Guid.TryParse would be better than either option. If this Guid is hard coded, and always valid, either of the above are perfectly reasonable options.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
13

I tried performance on one milion guids and Guid.Parse seems to be a insignificantly faster. It made 10-20 milisecods difference of 800 miliseconds of total creation on my PC.

public class Program
{
    public static void Main()
    {
        const int iterations = 1000 * 1000;
        const string input = "63559BC0-1FEF-4158-968E-AE4B94974F8E";

        var sw = Stopwatch.StartNew();
        for (var i = 0; i < iterations; i++)
        {
            new Guid(input);
        }
        sw.Stop();

        Console.WriteLine("new Guid(): {0} ms", sw.ElapsedMilliseconds);

        sw = Stopwatch.StartNew();
        for (var i = 0; i < iterations; i++)
        {
            Guid.Parse(input);
        }
        sw.Stop();

        Console.WriteLine("Guid.Parse(): {0} ms", sw.ElapsedMilliseconds);
    }
}

And output:

new Guid(): 804 ms

Guid.Parse(): 791 ms

tom.maruska
  • 1,411
  • 16
  • 22
  • 6
    This isn't a statistically significant benchmark. I ran this multiple times and got varying results. The background noise clouds any possible performance differences in these implementations. – Brad M Jan 23 '19 at 21:27
-2

In .Net framework v1.1 exists only 1 way -> var myguid = new Guid("9546482E-887A-4CAB-A403-AD9C326FFDA5");

Guid.Parse became available later.

  • 3
    Please provide an explanation along with solution so the user has a better understanding of what the code is doing. – Andrew Reese Sep 25 '20 at 16:12