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

后端 未结 5 1467
悲哀的现实
悲哀的现实 2020-12-29 19:24

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          


        
5条回答
  •  旧时难觅i
    2020-12-29 19:46

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

提交回复
热议问题