Guid.Parse()或新的GUID() - 有什么区别? [英] Guid.Parse() or new Guid() - What's the difference?

查看:1168
本文介绍了Guid.Parse()或新的GUID() - 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串转换为的System.Guid 这两种方式之间的区别是什么?是否有一个原因要选择一个比其他?

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

  VAR myguid =新的GUID(9546482E-887A-4CAB-A403-AD9C326FFDA5);
 

解决方案

就让我们来看看在反射显示,无论是pretty的多少等价的。

 公众的Guid(字符串G)
{
    如果(G == NULL)
    {
       抛出新ArgumentNullException(G);
    }
    该=空;
    GuidResult结果=新GuidResult();
    result.Init(GuidParseThrowStyle.All);
    如果(!TryParseGuid(G,GuidStyles.Any,裁判结果))
    {
        扔result.GetGuidParseException();
    }
    该= result.parsedGuid;
}

公共静态的Guid解析(字符串输入)
{
    如果(输入== NULL)
    {
        抛出新ArgumentNullException(输入);
    }
    GuidResult结果=新GuidResult();
    result.Init(GuidParseThrowStyle.AllButOverflow);
    如果(!TryParseGuid(输入,GuidStyles.Any,裁判结果))
    {
        扔result.GetGuidParseException();
    }
    返回result.parsedGuid;
}
 

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

解决方案

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

这篇关于Guid.Parse()或新的GUID() - 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆