67

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

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.

4
  • 2
    A point on nomenclature: ?? a null-coalescing operator. SomeValueType? is a nullable type. Commented Sep 21, 2012 at 9:47
  • @AdamHouldsworth Try answering as an regular answer!!! Commented Sep 21, 2012 at 9:47
  • You can try post on codereview.stackexchange.com Commented Sep 21, 2012 at 9:49
  • 4
    int? is not "an integer value". An "integer value" is never null, so the answer is simple: "it isn't". What you have is a Nullable<int>, or int? or just "a nullable int". Commented Sep 21, 2012 at 9:55

8 Answers 8

145

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

Also, nullable types support ==:

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Good answer; func fact, though: the parameterless age.GetValueOrDefault() is marginally faster - it completely bypasses the "do I have a value" check, and just returns the value field - which will have defaulted to default(T), aka 0.
@MarcGravell Cool, but for the purposes of the answer I chose to be explicit.
18

Several things:

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

?? 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:

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

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

Comments

4

Simply you can do this:

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

    }

Since C# version 9 you can do this:

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

  }

Or more readable:

  public void CheckNull(int? item)
  {
    if (item is not null)
    {
        //Do Something
    }

  }

Comments

2

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

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

Comments

2

Because int is a ValueType then you can use the following code:

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

or

if(Age.HasValue && Age != 0) or if (!Age.HasValue || Age == 0)

Comments

1

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,

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

is doubly invalid.

Comments

0

As of 2022 you can do it like this:

public int? Age;

if (Age is == 0)
{
   // do something
}

Comments

0

.ToString() method help you the same

public int? Age;

if (!string.IsNullOrEmpty(Age.ToString()))
{
// do something
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.