Why isn't there a Guid.IsNullOrEmpty() method

前端 未结 7 1276
不思量自难忘°
不思量自难忘° 2020-12-24 00:19

This keeps me wondering why Guid in .NET does not have IsNullOrEmpty() method (where empty means all zeros)

I need this at several places in my ASP.NET

7条回答
  •  时光说笑
    2020-12-24 00:47

    As others have pointed out, the premise of the question isn't all there. C# Guid is not nullable. However, Guid? is. A clean way of checking if a Guid? is null or Guid.Empty is by check if the result of GetValueOrDefault() is Guid.Empty. E.g.,

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

提交回复
热议问题