这两种将字符串转换为System.Guid的方法有什么区别?有理由 Select 其中一个吗?

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

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

推荐答案

快速查看反射器可以发现两者几乎是等效的.

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

.net相关问答推荐

.NET restore/build在使用组织包的Github Action工作流中调用时获得401

如何在AutoMapper中添加自定义方法到项目中?

如何运行大量阻塞/同步 I/O 操作

在 C# 中,如何使用泛型的基类将泛型接口的所有实例注入到单个构造函数中?

什么是提升运算符?

移位比Java中的乘法和除法更快吗? .网?

使用字典作为数据源绑定组合框

是否可以模拟 .NET HttpWebResponse?

如何 Select 数据表中列的最小值和最大值?

在 .NET C# 中存储加密密钥的最佳方式

Winforms:Application.Exit vs Environment.Exit vs Form.Close

HttpClient 和使用代理 - 不断得到 407

如何从 XDocument 获取 Xml 作为字符串?

单元测试 C# 保护方法

如何在多个解决方案之间共享相同的 Resharper 设置,无需人工干预?

POCO 是什么意思?

什么是 .NET 应用程序域?

如何在 nuspec 中指定特定的依赖版本?

例外:不支持 URI 格式

使用 C# 将时间跨度值转换为格式hh:mm Am/Pm