Guid为空判断

Guid类型的变量不会为空,初始化没有赋值的GUID应该是00000000-0000-0000-0000-000000000000 。

正确的判断应该是if(Guid testId== Guid.Empty)

如:

复制代码
  Guid guid = Guid.Parse("00000000-0000-0000-0000-000000000000"); 
            //Guid guid = Guid.Parse("E441C253-5080-4619-803A-00849D8CF710");  Console.WriteLine(guid); if (guid == Guid.Empty) { Console.WriteLine("GUID无效"); } else { Console.WriteLine("GUID有效"); } Console.Read();
复制代码

输出结果为:

 但是Guid?  guid = null  是可以的为空的, 判断方式:

      Guid? guid = null;
     if (guid.ToString() == "" || !guid.HasValue)
            {
                Console.WriteLine("GUID无效"); }

猜你喜欢

转载自www.cnblogs.com/chenze-Index/p/12888604.html