Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

Tiwari Vikram




Posted Questions



Wait...

Posted Answers



Answer


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.


Answer is posted for the following question.

How to check int for null c#?

Answer


Whole life insurance, or whole of life assurance, sometimes called "straight life" or "ordinary life," is a life insurance policy which is guaranteed to remain in force for the insured's entire lifetime, provided required premiums are paid, or to the maturity date.


Answer is posted for the following question.

What's a whole life insurance?

Answer


She is clothed with strength and dignity; she can laugh at the days to come. She speaks with wisdom, and faithful instruction is on her tongue. She watches over the affairs of her household and does not eat the bread of idleness. "Many women do noble things, but you surpass them all."


Answer is posted for the following question.

How to be a psalm 31 woman?


Wait...