A nullable type is any value type that also includes the null reference value. If we create a variable with nullable type, the variable can hold it's underlying type plus a null value.The reference type object previously has the ability to hold null type values,now with the introduction of nullable type value types also got the ability
Using nullable type we can determine whether a value type variable is empty, which interms can be done using isNullable property .
Nullable types can be defined in two ways
1) By simply placing a question mark after the type declaration, we can assign a null value
2) System.Nullable<ValueType>
Where valuetype is the type of value type eg: int
2) System.Nullable<ValueType>
Where valuetype is the type of value type eg: int
Each instance of a nullable type is defined by two read-only properties:
- 1. HasValue of type bool
- 2. Value of type ValueType.
int? i = null; // ‘?’ denotes i is Nullable
object o = i;
if (o == null)
Console.WriteLine("Correct behaviour …");
else
Console.WriteLine("Incorrect behaviour …");
object o = i;
if (o == null)
Console.WriteLine("Correct behaviour …");
else
Console.WriteLine("Incorrect behaviour …");
Examples
1) bool? isLoggedIn or System.Nullable<bool> isLoggedIn
By definition a bool variable can contain only two values, but if it is declared as a nullable type then it can accept 3 values
1)True
2)False
2)Null
2)False
2)Null
Most lifted operators (+, *, ˆ, <, . . . ) are null-strict: they give the result null if any argument is null.But the lifted strict logical operators (&) and (|) produce true or false whenever possible:
x&y | null | false | true |
---|---|---|---|
null | null | false | null |
false | false | false | false |
true | null | false | true |
x|y | null | false | true |
---|---|---|---|
null | null | null | true |
false | null | false | true |
true | true | true | true |
2) int? MyAge or System.Nullable<int> MyAge
This allows null values to be assigned to MyAge variable directly. This can be useful in scenario's in which we are using a stored procedure which can accept a null parameter input
eg:
create procedure MyProc
@MyAge int=null
.....
....
--do something
end
@MyAge int=null
.....
....
--do something
end
So from your codebehind you can pass this variable to Sql stored prcocedure without checking for null values
3)DateTime? dtServerTime or System.Nullable<datetime> dtServerTime
If an instance of a nullable type is initialized to null then its HasValue property returns false and its Value property raises an InvalidOperationException whenever an attempt is made to access its value. On the other hand, if an instance of a nullable type is initialized to a particular member of the underlying ValueType then its HasValue property returns true and its Value property returns the member itself.
isNullable Property
isNullable Indicates whether the property is nullableBoxing and nullable types
At the last moment (summer 2005), Microsoft changed the design of boxing of nullable value types:
- Boxing of a null value of type int? produces a null reference.
- Boxing of a non-null value of type int? produces a boxed int – not a boxed int?.
Example:
int? bi1 = null, bi2 = 17;
Object o1 = bi1, o2 = bi2;
Object o1 = bi1, o2 = bi2;
Now o1 would be a null reference, and o2 would be a boxed 17.
As an interesting consequence, a boxed int can be turned into an int?:
Object o1 = null, o2 = 17;
int? bi1 = (int?)o1, bi2 = (int?)o2;
int? bi1 = (int?)o1, bi2 = (int?)o2;
Now bi1.HasValue would be false, and bi2.HasValue would be true.
This design makes null comparison work as expected, when a generic type type parameter is instantiated with a nullable type:
class C<t> {
public bool M(T x) { return x == null; }
}
... new C<int?>.M(null) returns true as expected ...
public bool M(T x) { return x == null; }
}
... new C<int?>.M(null) returns true as expected ...
0 comments
Post a Comment