Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

How to check int for null c#?

3 Answer(s) Available
Answer # 1 #

Since C# version 7 you can use the is keyword for the null check like in the snippet below:

But with C# 7, there’s even a shorter syntax. Discards were also introduced. They are unused and ignored variables that are represented in your code with an underscore (_). In combination with the Null-coalescing operator (??) you can write the null check like this:

That means, the whole method looks just like this:

To be honest, I really like the last approach using the discards, but maybe for some developers it’s too much. I think the is keyword is very clear and readable. It is my favorite.

The is keyword has also the big advantage that it ignores any ==/!= operator overloads on the specific class. It will do a null check, no matter if there’s an operator overload or not. That makes it better than just using ==. You can read more about this in this blog post.

With C# 9.0, you can combine the is expression with the logical not pattern, which is powerful if you want to check if an object is NOT null. Before C# 9.0 you had to use the is expression like below to check if an object is not null:

Some developers preferred the following syntax to check if the name is not null:

But the statements above are neither very readable nor easy to understand. That’s why many developers still prefer the classic way:

But since C# 9.0, you can write that not null check like below, and I think that is really readable code:

So, with C# 9.0, you can write your null / not-nulll checks like below, and I think that’s readable:

Happy coding,Thomas

[4]
Edit
Query
Report
Aishwarya Clement
USED CAR RENOVATOR
Answer # 2 #
  • using System.IO;
  • using System;
  • class NullableType.
  • {
  • static void Main(string[] args)
  • {
  • int ? myInt1 = 15;
  • int ? myInt2 = null;
[3]
Edit
Query
Report
Jose Hardmon
Fettler: Railway Maintenance Worker
Answer # 3 #

As you know, a value type cannot be assigned a null value. For example, int i = null will give you a compile time error.

C# 2.0 introduced nullable types that allow you to assign null to value type variables. You can declare nullable types using Nullable where T is a type.

A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, Nullable can be assigned any value from -2147483648 to 2147483647, or a null value.

The Nullable types are instances of System.Nullable struct. Think it as something like the following structure.

A nullable of type int is the same as an ordinary int plus a flag that says whether the int has a value or not (is null or not). All the rest is compiler magic that treats "null" as a valid value.

The HasValue returns true if the object has been assigned a value; if it has not been assigned any value or has been assigned a null value, it will return false.

Accessing the value using NullableType.value will throw a runtime exception if nullable type is null or not assigned any value. For example, i.Value will throw an exception if i is null:

Use the GetValueOrDefault() method to get an actual value if it is not null and the default value if it is null. For example:

You can use the '?' operator to shorthand the syntax e.g. int?, long? instead of using Nullable.

Use the '??' operator to assign a nullable type to a non-nullable type.

In the above example, i is a nullable int and if you assign it to the non-nullable int j then it will throw a runtime exception if i is null. So to mitigate the risk of an exception, we have used the '??' operator to specify that if i is null then assign 0 to j.

A nullable type has the same assignment rules as a value type. It must be assigned a value before using it if nullable types are declared in a function as local variables. If it is a field of any class then it will have a null value by default.

For example, the following nullable of int type is declared and used without assigning any value. The compiler will give "Use of unassigned local variable 'i'" error:

In the following example, a nullable of int type is a field of the class, so it will not give any error.

Null is considered to be less than any value. So comparison operators won't work against null. Consider the following example where i is neither less than j, greater than j nor equal to j:

Nullable static class is a helper class for Nullable types. It provides a compare method to compare nullable types. It also has a GetUnderlyingType method that returns the underlying type argument of nullable types.

[0]
Edit
Query
Report
Tiwari Vikram
Aircraft Launch and Recovery Officer