在 c# 中检查整数值是否为 Null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12528107/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:35:09  来源:igfitidea点击:

Check an integer value is Null in c#

c#.net

提问by

I have got an integer value and i need to check if it is NULL or not. I got it using a null-coalescing operator

我有一个整数值,我需要检查它是否为 NULL。我使用空合并运算符得到它

C#:

C#:

public int? Age;

if ((Age ?? 0)==0)
{
   // do somethig
}

Now i have to check in a older application where the declaration part is not in ternary. So, how to achieve this without the null-coalescing operator.

现在我必须检查一个较旧的应用程序,其中声明部分不是三元的。那么,如何在没有空合并运算符的情况下实现这一点。

采纳答案by Adam Houldsworth

Nullable<T>(or ?) exposes a HasValueflag to denote if a value is set or the item is null.

Nullable<T>(或?) 公开一个HasValue标志以表示是否设置了值或项目是否为空。

Also, nullable types support ==:

此外,可为空类型支持==

if (Age == null)

if (Age == null)

The ??is the null coalescing operator and doesn't result in a boolean expression, but a value returned:

??是空合并运算符,不会导致布尔表达式,但返回值:

int i = Age ?? 0;

So for your example:

所以对于你的例子:

if (age == null || age == 0)

Or:

或者:

if (age.GetValueOrDefault(0) == 0)

Or:

或者:

if ((age ?? 0) == 0)

Or ternary:

或三元:

int i = age.HasValue ? age.Value : 0;

回答by Oded

Several things:

几件事:

Ageis not an integer - it is a nullableinteger type. They are not the same. See the documentation for Nullable<T>on MSDN for details.

Age不是整数 - 它是空的整数类型。她们不一样。有关Nullable<T>详细信息,请参阅MSDN 上的文档。

??is the null coalesce operator, not the ternary operator (actually called the conditional operator).

??空合并运算符,而不是三元运算符(实际上称为条件运算符)。

To check if a nullable type has a value use HasValue, or check directly against null:

要检查可空类型是否具有值HasValue,请使用,或直接检查null

if(Age.HasValue)
{
   // Yay, it does!
}

if(Age == null)
{
   // It is null :(
}

回答by Smileek

There is already a correct answer from Adam, but you have another option to refactor your code:

Adam 已经给出了正确的答案,但您还有另一种选择来重构您的代码:

if (Age.GetValueOrDefault() == 0)
{
    // it's null or 0
}

回答by Jodrell

As stated above, ??is the null coalescing operator. So the equivalent to

如上所述,??是空合并运算符。所以相当于

(Age ?? 0) == 0

without using the ??operator is

不使用??运算符是

(!Age.HasValue) || Age == 0


However, there is no version of .Net that has Nullable< T >but not ??, so your statement,

但是,没有具有Nullable< T >但没有的 .Net 版本??,因此您的声明,

Now i have to check in a older application where the declaration part is not in ternary.

现在我必须检查一个较旧的应用程序,其中声明部分不是三元的。

is doubly invalid.

是双重无效的。

回答by Fereydoon Barikzehy

Simply you can do this:

你可以这样做:

    public void CheckNull(int? item)
    {
        if (item != null)
        {
            //Do Something
        }

    }

回答by MSD561

Because intis a ValueType then you can use the following code:

因为int是一个 ValueType 那么你可以使用以下代码:

if(Age == default(int) || Age == null)