Guid.Parse() or new Guid() - What's the difference?

brennazoon picture brennazoon · Aug 2, 2011 · Viewed 25.6k times · Source

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");

Answer

Jakub Konecki picture Jakub Konecki · Aug 2, 2011

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;
}