这两种将字符串转换为System.Guid
的方法有什么区别?有理由 Select 其中一个吗?
var myguid = Guid.Parse("9546482E-887A-4CAB-A403-AD9C326FFDA5");
或
var myguid = new Guid("9546482E-887A-4CAB-A403-AD9C326FFDA5");
这两种将字符串转换为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;
}