C# IsNullOrEmpty 与对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8887570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IsNullOrEmpty with Object
提问by Nate Pet
IsNullOrEmpty is used with strings to check if a string is null or empty. Is there an equivalent with an object to see if any object is null or not? I assume we can do
IsNullOrEmpty 与字符串一起使用以检查字符串是 null 还是空。是否有一个对象的等价物来查看任何对象是否为空?我想我们可以做到
obj1 != null
but not sure if there is any other way...
但不知道有没有其他办法...
采纳答案by Sam I am says Reinstate Monica
a null string is null, an empty string is ""
空字符串为空,空字符串为“”
isNullOrEmpty requires an intimate understanding about the implementation of a string. If you want one, you can write one yourself for your object, but you have to make your own definition for whether your object is "empty" or not.
isNullOrEmpty 需要对字符串的实现有深入的了解。如果你想要一个,你可以为你的对象自己写一个,但是你必须自己定义你的对象是否“空”。
ask yourself: What does it mean for an object to be empty?
问问自己:一个对象是空的意味着什么?
回答by Etienne de Martel
Why would you need any other way? Comparing an Objectreference with nullis the least-verbose way to check if it's null.
为什么你需要任何其他方式?比较Object引用null是检查它是否为空的最简单的方法。
回答by Andrew Cooper
obj1 != null
is the right way.
是正确的方法。
String defines IsNullOrEmptyas a nicer way to say
字符串定义IsNullOrEmpty为一种更好的说法
obj1 == null || obj == String.Empty
so it does more than just check for nullity.
所以它不仅仅是检查无效性。
There may be other classes that define a method to check for a sematically "blank or null" object, but that would depend on the semantics of the class, and is by no means universal.
可能还有其他类定义了一种方法来检查语义上的“空白或空”对象,但这取决于类的语义,并且绝不是通用的。
It's also possible to create extension method to do this kind of thing if it helps the readability of your code. For example, a similar approach to collections:
如果它有助于代码的可读性,也可以创建扩展方法来执行此类操作。例如,类似的集合方法:
public static bool IsNullOrEmpty (this ICollection collection)
{
return collection == null || collection.Count == 0;
}
回答by Devin Burke
IsNullOrEmptyis essentially shorthand for the following:
IsNullOrEmpty基本上是以下内容的简写:
return str == null || str == String.Empty;
So, no there is no function that just checks for nulls because it would be too simple. obj != nullis the correct way. But you can create such a (superfluous) function yourself using the following extension:
所以,没有任何函数只检查空值,因为它太简单了。obj != null是正确的方法。但是您可以使用以下扩展自己创建这样一个(多余的)函数:
public bool IsNull(this object obj)
{
return obj == null;
}
Then you are able to run anyObject.IsNull().
然后你就可以运行了anyObject.IsNull()。
回答by Marius Schulz
The following code is perfectly fine and the right way(most exact, concise, and clear) to check if an object is null:
以下代码非常好,并且是检查对象是否为正确的方法(最准确、简洁和清晰)null:
object obj = null;
//...
if (obj == null)
{
// Do something
}
String.IsNullOrEmptyis a method existing for convenience so that you don't have to write the comparison code yourself:
String.IsNullOrEmpty是一种为了方便而存在的方法,这样您就不必自己编写比较代码:
private bool IsNullOrEmpty(string input)
{
return input == null || input == string.Empty;
}
Additionally, there is a String.IsNullOrWhiteSpacemethod checking for nulland whitespace characters, such as spaces, tabs etc.
此外,还有String.IsNullOrWhiteSpace一种检查null空格字符的方法,例如空格、制表符等。
回答by Zorgarath
I found that DataGridViewTextBoxvalues and some JSON objects don't equal Null but instead are "{}"values. Comparing them to Null doesn't work but using these do the trick:
我发现DataGridViewTextBox值和一些 JSON 对象不等于 Null,而是"{}"值。将它们与 Null 进行比较不起作用,但使用这些可以解决问题:
if (cell.Value is System.DBNull)
if (cell.Value == System.DBNull.Value)
A good excerpt I found concerning the difference between Null and DBNull:
我发现了一个关于 Null 和 DBNull 之间区别的很好的摘录:
Do not confuse the notion of null in an object-oriented programming language with a DBNull object. In an object-oriented programming language, null means the absence of a reference to an object. DBNull represents an uninitialized variant or nonexistent database column.
不要将面向对象编程语言中的 null 概念与 DBNull 对象混淆。在面向对象的编程语言中,null 表示不存在对对象的引用。DBNull 表示未初始化的变体或不存在的数据库列。
You can learn more about the DBNull class here.
回答by user13429420
object MyObject = null;
对象 MyObject = null;
if (MyObject != null && !string.IsNullOrEmpty(MyObject.ToString())) { ... }
if (MyObject != null && !string.IsNullOrEmpty(MyObject.ToString())) { ... }

