【问题标题】:Check if Nullable Guid is empty in c#在 c# 中检查 Nullable Guid 是否为空
【发布时间】:2013-07-17 07:49:59
【问题描述】:

引用this问题的答案。

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

如果我看到这个怎么办?

public Nullable<System.Guid> SomeProperty { get; set; }

我应该如何检查这是否为空?像这样?

(SomeProperty == null)

还是这样?

(SomeProperty == Guid.Empty)

【问题讨论】:

标签: c# nullable guid


【解决方案1】:

如果你想确定你需要检查两个

SomeProperty == null || SomeProperty == Guid.Empty

因为它可以是 null 'Nullable' 并且它可以是一个空的 GUID,例如 {00000000-0000-0000-0000-000000000000}

【讨论】:

  • 确认:Guid.Empty.ToString() == "00000000-0000-0000-0000-000000000000"
  • Guid 永远不会为空,但它可以是 Guid.Empty
  • Guid 本身不是,但如果您阅读问题,它是 Nullable Guid,而 Nullable 的漏洞在于它可以为 null ^^
  • 这是正确答案;请注意,如果您从输入序列化 Guid,如果您使用 non-nullable 变体,null 将序列化为 Guid.Empty,这可以为您节省 null,请在此处查看.
【解决方案2】:

SomeProperty.HasValue我想这就是你要找的东西。

请参阅 DevDave 或 Sir l33tname 的回答。

编辑:顺便说一句,你可以写 System.Guid? 而不是 Nullable&lt;System.Guid&gt; ;)

【讨论】:

  • 这只是使用 Nullable Guid 时的正确答案。我猜大多数人使用Guid? 而不是Guid
  • 不够。空 Guid 与空 Guid(即 Guid.Empty)不同。所以你不仅需要检查 Guid?.HasValue,还需要检查 Guid?.Value == Guid.Empty。
  • 正如@Triynko 所说,这个答案需要更新以检查 Guid.Empty
  • 这错误地回答了问题。
【解决方案3】:

请注意,HasValue 将为空的 Guid 返回 true。

bool validGuid = SomeProperty.HasValue &amp;&amp; SomeProperty != Guid.Empty;

【讨论】:

    【解决方案4】:

    查看Nullable&lt;T&gt;.HasValue

    if(!SomeProperty.HasValue ||SomeProperty.Value == Guid.Empty)
    {
     //not valid GUID
    }
    else
    {
     //Valid GUID
    }
    

    【讨论】:

      【解决方案5】:

      您应该使用HasValue 属性:

      SomeProperty.HasValue

      例如:

      if (SomeProperty.HasValue)
      {
          // Do Something
      }
      else
      {
          // Do Something Else
      }
      

      仅供参考

      public Nullable<System.Guid> SomeProperty { get; set; }
      

      相当于:

      public System.Guid? SomeProperty { get; set; }
      

      MSDN 参考: http://msdn.microsoft.com/en-us/library/sksw8094.aspx

      【讨论】:

        【解决方案6】:

        从 C# 7.1 开始,当编译器可以推断表达式类型时,您可以使用默认文字来生成类型的默认值。

        Console.Writeline(default(Guid));  
           // ouptut: 00000000-0000-0000-0000-000000000000
        
        Console.WriteLine(default(int));  // output: 0
        
        Console.WriteLine(default(object) is null);  // output: True
        

        https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/default

        【讨论】:

          【解决方案7】:

          您可以创建一个扩展方法来验证 GUID。

          public static class Validate
          {
              public static void HasValue(this Guid identity)
              {
                  if (identity ==  null || identity == Guid.Empty)
                      throw new Exception("The GUID needs a value");
              }
          }
          

          并使用扩展

              public static void Test()
              {
                  var newguid = Guid.NewGuid();
          
                  newguid.HasValue();
              }
          

          【讨论】:

          • 我不会走这条路。您的方法名称是一个真/假问题,因此它应该返回一个布尔值。我怀疑有很多代码库在检查值时会抛出异常,尤其是当可空类型完全可以传入时
          猜你喜欢
          • 2021-09-29
          • 1970-01-01
          • 2010-11-01
          • 1970-01-01
          • 2013-12-16
          • 1970-01-01
          • 1970-01-01
          • 2011-05-03
          • 1970-01-01
          相关资源
          最近更新 更多