【问题标题】:Why isn't there a Guid.IsNullOrEmpty() method为什么没有 Guid.IsNullOrEmpty() 方法
【发布时间】:2012-03-23 10:27:14
【问题描述】:

这让我想知道为什么 .NET 中的 Guid 没有 IsNullOrEmpty() 方法(其中空表示全为零)

在编写 REST API 时,我在 ASP.NET MVC 代码中的多个地方都需要它。

或者我错过了什么,因为互联网上没有人提出同样的要求?

【问题讨论】:

标签: c# asp.net-mvc


【解决方案1】:

Guid 是一个value type,所以Guid 类型的变量一开始不能为空。如果您想知道它是否与空 guid 相同,您可以使用:

if (guid == Guid.Empty)

【讨论】:

  • Entity Framework 将 Guid 变量定义为 Guid 的情况如何? (可为空)?
  • @goths:那么你可以使用if (nullableGuid == null || nullableGuid == Guid.Empty)... 或者如果你真的想要创建你自己的扩展方法。大概它很少出现,以至于对大多数人来说不值得。
  • @goths 您可以为所有值类型制作通用扩展方法。例如:public static bool IsNullOrDefault<T>(this T? self) where T : struct { return !self.HasValue || self.Value.Equals(default(T)); }
【解决方案2】:

一方面,Guid 不能为空。您可以检查:

myGuid == default(Guid)

相当于:

myGuid == Guid.Empty

【讨论】:

  • 从 c# 7.1 开始,您可以使用 myGuid == default
【解决方案3】:

这是一个可以为空的 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);
}

更新

如果您真的想在任何地方使用它,您可以为常规 Guid 编写另一个扩展方法。它永远不能为空,所以有些人不会喜欢这个......但它可以满足您正在寻找的目的,并且您不必知道您是否正在使用 Guid?或 Guid(适合重构等)。

/// <summary>
/// Determines if Guid is Guid.Empty
/// </summary>
public static bool IsNullOrEmpty(this Guid guid)
{
  return (guid == Guid.Empty);
}

现在您可以在所有情况下使用someGuid.IsNullOrEmpty();,无论您使用的是 Guid 还是 Guid?。

就像我说的,有些人会抱怨命名,因为IsNullOrEmpty() 暗示该值可以为空(当它不能为空时)。如果你真的想,为扩展名想一个不同的名字,比如 IsNothing()IsInsignificant() 或其他什么:)

【讨论】:

    【解决方案4】:

    您可以对 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
        }
    }
    

    【讨论】:

    • g = new Guid() 实际上会创建一个空的 guid。你打算写g = Guid.NewGuid() 吗?
    【解决方案5】:

    正如其他人所指出的,问题的前提并不全部存在。 C# Guid 不可为空。但是,Guid? 是。检查Guid?null 还是Guid.Empty 的一种干净方法是检查GetValueOrDefault() 的结果是否为Guid.Empty。例如,

    Guid? id;
    
    // some logic sets id
    
    if (Guid.Empty.Equals(guid.GetValueOrDefault()))
    {
        // Do something
    }
    

    【讨论】:

      【解决方案6】:

      这是一个可空结构类型的通用扩展类:

      public static class NullableExtensions
      {
          public static bool IsNullOrDefault<T>(this T? self) where T : struct
          {
              return !self.HasValue || self.Value.Equals(default(T));
          }
      }
      

      【讨论】:

        【解决方案7】:

        你知道我经常看到这样的陈述

        Guid是值类型,所以Guid类型的变量不能为null 开始

        但这不是真的。

        同意您不能以编程方式将 Guid 设置为 null,但是当某些 SQL 引入 UniqueIdentifier 并将其映射到 Guid 时,如果该值在 db 中为 null,则该值在 C# 中显示为 null。

        【讨论】:

        • 如果该字段在数据库中可以为空,那么指定类型可以为空(Guid?)会更正确
        猜你喜欢
        • 1970-01-01
        • 2011-07-16
        • 2015-05-01
        • 2014-03-07
        • 2011-06-12
        • 2013-01-18
        • 2019-04-08
        • 2015-11-26
        • 2010-11-24
        相关资源
        最近更新 更多