C# 为什么没有 Guid.IsNullOrEmpty() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9837602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why isn't there a Guid.IsNullOrEmpty() method
提问by Gautam Jain
This keeps me wondering why Guid in .NET does not have IsNullOrEmpty()method (where empty means all zeros)
这让我想知道为什么 .NET 中的 Guid 没有IsNullOrEmpty()方法(其中空表示全为零)
I need this at several places in my ASP.NET MVC code when writing the REST API.
在编写 REST API 时,我在 ASP.NET MVC 代码的几个地方需要这个。
Or am I missing something because nobody on the Internet has asked for the same?
或者我错过了什么,因为互联网上没有人提出同样的要求?
采纳答案by Jon Skeet
Guidis a value type, so a variable of type Guidcan't be null to start with. If you want to know if it's the same as the empty guid, you can just use:
Guid是值类型,因此类型的变量一Guid开始不能为空。如果您想知道它是否与空 guid 相同,您可以使用:
if (guid == Guid.Empty)
回答by dxh
For one thing, Guidis not nullable. You could check:
一方面,Guid不可为空。你可以检查:
myGuid == default(Guid)
which is equivalent to:
这相当于:
myGuid == Guid.Empty
回答by SimpleVar
You can make an extension method to Guid to add IsEmpty functionality:
您可以为 Guid 制作一个扩展方法来添加 IsEmpty 功能:
public static class GuidEx
{
public static bool IsEmpty(this Guid guid)
{
return guid == Guid.Empty;
}
}
public class MyClass
{
public void Foo()
{
Guid g;
bool b;
b = g.IsEmpty(); // true
g = Guid.NewGuid();
b = g.IsEmpty; // false
b = Guid.Empty.IsEmpty(); // true
}
}
回答by Justin
Here is a simple extension method for a nullable Guid.
这是可空 Guid 的简单扩展方法。
/// <summary>
/// Determines if a nullable Guid (Guid?) is null or Guid.Empty
/// </summary>
public static bool IsNullOrEmpty(this Guid? guid)
{
return (!guid.HasValue || guid.Value == Guid.Empty);
}
UPDATE
更新
If you really wanted to use this everywhere you could write another extension method for a regular Guid. It can never be null, so some people won't like this... but it serves the purpose you are looking for and you don't have to know whether you are working with Guid? or Guid (nice for re-factoring etc.).
如果你真的想在任何地方使用它,你可以为常规 Guid 编写另一个扩展方法。它永远不能为空,所以有些人不会喜欢这个……但它满足了你正在寻找的目的,你不必知道你是否正在使用 Guid?或 Guid(适合重构等)。
/// <summary>
/// Determines if Guid is Guid.Empty
/// </summary>
public static bool IsNullOrEmpty(this Guid guid)
{
return (guid == Guid.Empty);
}
Now you could use someGuid.IsNullOrEmpty();in all cases, whether you are using Guid or Guid?.
现在您可以someGuid.IsNullOrEmpty();在所有情况下使用,无论您是使用 Guid 还是 Guid?。
Like I said, some people will complain about the naming because IsNullOrEmpty()implies that the value could be null (when it can't). If you really wanted to, come up with a different name for the extensions like IsNothing()or IsInsignificant()or whatever :)
就像我说的那样,有些人会抱怨命名,因为这IsNullOrEmpty()意味着该值可能为空(当它不能时)。如果你真的想,想出一个不同的扩展名,比如IsNothing()或IsInsignificant()或其他:)
回答by Paul Gorbas
You know I see this statements like this one all the time
你知道我经常看到这样的声明
Guid is a value type, so a variable of type Guid can't be null to start with
Guid 是值类型,因此 Guid 类型的变量不能以 null 开头
but it is just NOT TRUE.
但这不是真的。
Agreed you can not programmatic set a Guid to null, but when some SQL pulls in a UniqueIdentifier and maps it to a Guid, and if that value is null in the db, the value comes up as null in the C#.
同意您不能以编程方式将 Guid 设置为 null,但是当某些 SQL 提取 UniqueIdentifier 并将其映射到 Guid 时,如果该值在 db 中为 null,则该值在 C# 中显示为 null。
回答by Craig Niles
As others have pointed out, the premise of the question isn't all there. C# Guidis not nullable. However, Guid?is. A clean way of checking if a Guid?is nullor Guid.Emptyis by check if the result of GetValueOrDefault()is Guid.Empty. E.g.,
正如其他人所指出的那样,问题的前提并不完整。C#Guid不可为空。然而,Guid?是。检查是否一个的清洁方式Guid?是null或者Guid.Empty是通过检查,如果结果GetValueOrDefault()是Guid.Empty。例如,
Guid? id;
// some logic sets id
if (Guid.Empty.Equals(guid.GetValueOrDefault()))
{
// Do something
}

