检查数据是否为空。 [英] Check if is data is empty.

查看:91
本文介绍了检查数据是否为空。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 //代表字符串 

如果(carName.Length == 0
returnMessage + = Car Name不能为空!


//如何检查如果 为空 for dateTime

解决方案

除了Sergey给你的宝贵建议外,我想在'DateTime上添加一些评论。



1. DateTime是一个结构:它的行为类似于一个值类型:没有引用它是可能的。因此,它永远不会为空。



2.未初始化的DateTime具有默认值DateTime.MinValue:1/1/0001 12:00: 00 AM,及其TimeOfDay属性的默认值为:00:00:00



3.程序员经常使用将DateTime.MinValue与DateTime进行比较(非-nullable)来测试一个无效或缺失的日期。



4.当你使用可以为空的DateTime时,会发生一些有趣的事情:考虑这些声明

日期时间? DT1; 
DateTime dt2;
DateTime dt3 = new DateTime( 0 );
DateTime? dt4 = DateTime.MinValue;
DateTime? dt5 = new DateTime( 2014 1 31 6 0 ,< span class =code-digit> 0 ,DateTimeKind.Utc);

考虑:

  bool  isMinDate1 = dt1 == DateTime.MinValue;  //   false  
bool isMinDate2 = dt2 == DateTime.MinValue; // true
bool isMinDate3 = dt3 == DateTime.MinValue; // true
bool isMinDate4 = dt4 == DateTime.MinValue; // true
bool isMinDate5 = dt5 == DateTime.MinValue; // false

如果您尝试访问DateTime字段,例如TimeOfDay,可以为空的DateTime:即使可以为空的DateTime包含有效的DateTime实例,也会出现错误,而不是null!



如果显示IntelliSense,您会注意到对于可为空的DateTime,您只获得基本的Object属性,但是您确实获得了Value Field。对于不可为空的DateTime,没有'Value Field。



然而,如果可以为空,那么使用'Value Field:就会出现陷阱 DateTime变量是未初始化的,就像上面代码中的'dt1一样,如果您尝试访问它,您的应用程序将因System.InvalidOperationException错误而中断。



所以,根据常规与可以为空的DateTime类型中的这些差异来做出选择。



并且,请记住DateTime?实际上是Nullable< DateTime> ...见:[ ^ 。可空< T>泛型类型在.NET 2.0中实现。



您可能想要查看.NET中的值与引用类型:[ ^ ]。


在Bill和SA解决方案之上。

也试一试。可能对你有帮助。



Nullable DateTime

  //  使用Nullable DateTime处理 
DateTime? dateTimeNullable = null ; // 可以指定空值
if (dateTimeNullable.HasValue) // 使用HasValue属性检查它是否有值
return dateTimeNullable.Value;





不可为空DateTime

  //  处理DateTime对象没有Nullable  
DateTime dateTime = DateTime.MinValue; // 无法分配空值,因为它是值类型,您可以指定最小值或默认值
if (dateTime!= DateTime.MinValue) // 对于值类型,您可以使用最小值或默认值进行检查
return dateTimeNullable;


Matt在评论中给了你一些想法,但是,如果确实需要清楚地指出无约会条件(或者更好)说,没时间),你应该使用略有不同的类型。而不是 System.DateTime ,使用 nullable System.DateTime?。它会给你一些不同的维度:

 System.DateTime? time =  //   ...  

// ...

if ( time!= null ){
SystemDateTime myTime = time.Value;
// ...
} else
DoSomethingElse();





我希望你明白了。如果没有,请阅读有关可空数据类型以及如何使值类型可空的内容。



-SA

//for string

if (carName.Length == 0)
returnMessage += Car Name cannot be blank!


//how you check if is empty for dateTime

解决方案

In addition to the valuable advice given to you by Sergey, I'd like to add some comments on 'DateTime.

1. DateTime is a Struct: it behaves like a Value Type: no "reference" to it is possible. So, it can never be null.

2. An un-initialized DateTime has the default value of DateTime.MinValue: "1/1/0001 12:00:00 AM," and its TimeOfDay Property the default value: "00:00:00"

3. Programmers do often use comparing DateTime.MinValue with a DateTime (non-nullable) to test for an invalid, or missing, date.

4. When you use a nullable DateTime, some interesting things happen: consider these declarations

DateTime? dt1;
DateTime dt2;
DateTime dt3 = new DateTime(0);
DateTime? dt4 = DateTime.MinValue;
DateTime? dt5 = new DateTime(2014, 1, 31, 6, 0, 0, DateTimeKind.Utc);

Consider:

bool isMinDate1 = dt1 == DateTime.MinValue; // false
bool isMinDate2 = dt2 == DateTime.MinValue; // true
bool isMinDate3 = dt3 == DateTime.MinValue; // true
bool isMinDate4 = dt4 == DateTime.MinValue; // true
bool isMinDate5 = dt5 == DateTime.MinValue; // false

If you try and access DateTime Fields, like TimeOfDay, in a nullable DateTime: you will get an error, even if the nullable DateTime contains a valid DateTime instance, not null !

You'll note that if you display the IntelliSense for a nullable DateTime you get only the basic Object properties, but you do get a 'Value Field. For a non-nullable DateTime, there is no 'Value Field.

However, there's a "gotcha" in using the 'Value Field: if the nullable DateTime variable is un-initialized, like 'dt1 in the code above, your app will break with a System.InvalidOperationException error if you try and access it.

So, make your choice based on consideration of these differences in "regular" vs. nullable DateTime Types.

And, keep in mind that a DateTime? is really a Nullable<DateTime> ... see: [^]. Nullable<T> generic Types were implemented in .NET 2.0.

You might want to review Value vs. Reference Types in .NET: [^].


On Top of Bill and SA solution.
try this too. might help you.

Nullable DateTime

// Handling with Nullable  DateTime
            DateTime? dateTimeNullable = null;  // null values can be assigned
            if (dateTimeNullable.HasValue)       //use HasValue property to check it has value or not
                return dateTimeNullable.Value;



Non Nullable DateTime

//Handling DateTime object without Nullable
DateTime dateTime = DateTime.MinValue;  // null values cannot be assigned since it as value type, u can assign min value or default value
if ( dateTime != DateTime.MinValue)  // for value type you can check with min value or default value
    return dateTimeNullable;


Matt gave you some idea in your comment, but, if really need to clearly indicate the "no-date" condition (or, better say, "no time"), you should use slightly different type. Instead of System.DateTime, use nullable System.DateTime?. It would give you some different dimension:

System.DateTime? time = //...

//...

if (time != null) {
   SystemDateTime myTime = time.Value;
   //...
} else
   DoSomethingElse();



I hope you got the idea. If not, read about nullable data types and on how to make value types nullable.

—SA


这篇关于检查数据是否为空。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆