The data types introduced as part of .NET 2.0 that permit the storage of null information. we are going to discuss about them.the variable which are undefined or contain nullable data.
Null Value
When a program works with numeric information, particularly when it utilises information in a database, it is often the case that a value is undefined. An example of this is when a series of simple yes / no questions is asked and the result of each question held in a Boolean format. After a question is answered, the Boolean value can be set to either true or false to indicate the result. However, before the answer is given what should the value hold? The answer is null.
Nullable Numeric Data Types
Null is a special value that represents information that has not yet been defined. in earlier version the null value exists but could not be applied to numeric variables In .NET framework 2.0, Microsoft rectified this problem by introducing nullable versions of these data types.
All of the basic numeric data types have nullable equivalents. There are several ways to declare a variable as a nullable type. The simplest and most readable method is to simply append a question mark (?) to the data type. The following example shows the declaration and assignment of several nullable variables:
int? nullableInt;
int? nullValue = null;
int? notNull = 123;
bool? answer1 = true;
bool? answer2 = false;
bool? answer3 = null;
You can see from the above example that creating a nullable variable is similar to creating a standard numeric variable. As with other numeric variables, a value must be assigned to a variable before it is used, even if that value is null. The following code produces an error if you attempt to compile it.
int? nullableInt;
int? copy = nullableInt; // Invalid as nullableInt is not yet assigned
Data Type Conversion
Numeric nullable data types include very similar implicit and explicit conversion between the various sizes of nullable integers and floating point values. Values can also be converted between their nullable and non-nullable versions. As you would expect, conversion between two incompatibly sized types requires a cast statement, as does casting from a nullable to a non-nullable type.
int standardInteger = 123;
int? nullableInteger;
decimal standardDecimal = 12.34M;
// Implicit conversion from int to int?
nullableInteger = standardInteger;
// Explicit conversion from int? to int
standardInteger = (int)nullableInteger;
// Explicit cast from decimal to int?
nullableInteger = (int?)standardDecimal;
Care must be taken when casting a nullable value as a non-nullable data type. If the value of the nullable data type is null, this cannot be represented in the destination value and a run-time error will occur. This can be avoided by checking if the value is set to null before attempting the conversion.
Arithmetic Operators
The standard arithmetic operators can be used with numeric nullable data types. However, if the value of any of the operands is null, the result will always be null regardless of any other values.
int? a = 55;
int? n = null;
int? result;
result = a * 2; // result = 110
result = a * n; // result = null
Boolean Operators
When using nullable Boolean data types, the binary standard Boolean logical operators can be used. Where both of the operands used are set to either true or false, the results of the operation are exactly the same as for non-nullable Booleans. Where one or both of the operands used in a logical operation are set to null, the result is usually null. There are two special cases where this does not happen. In a logic OR operation, if any value is true then the result is true, even if the other operand is null. For logical AND operations, if either value is false then the result is also false.
bool? result;
result = true & null; // result = null
result = false & null; // result = false
result = true | null; // result = true
result = false | null; // result = null
result = true ^ null; // result = null
result = false ^ null; // result = null
Relational Operators
The relational operators are all valid for use with nullable numeric data types. However, when the value being compared is null, the results are not always as expected. The equal to and not equal to operators are able to make comparisons with both numeric and null values. With all of the other relational operators, the result of the comparison is always false when a value being compared is null.
int? a = 55;
int? n = null;
bool result;
result = a == n; // result = false
result = a != n; // result = true
result = n == null; // result = true
result = a > n; // result = false
result = a < n; // result = false
Testing for Null Values
The previous section showed the use of the relational operators with numeric nullable types. Included in the examples you can see that it is possible to use the equal to or not equal to operators to test if the value of a variable is null. In addition to these operators, the nullable data types define several properties and methods for checking if the value is null and for retrieving the value where it is not.
HasValue Property
The first property of the numeric nullable types of interest is the HasValue property. This property simply returns a Boolean value indicating whether the nullable variable contains a real value or a null value. To access the value of a property, the member access operator is used. This is simply a full stop (period or dot) placed between the name of the variable and the name of the member (property or method) to be used. The following example shows the HasValue property used to set a non-nullable value to the value of a nullable type with a default value of -1 where the nullable variable has no value.
int? a = 10;
int? n = null;
int result;
bool checkIfNull;
checkIfNull = a.HasValue; // checkIfNull = true
result = checkIfNull ? (int)a : -1; // result = 10
checkIfNull = n.HasValue; // checkIfNull = false
result = checkIfNull ? (int)n : -1; // result = -1
Value Property
The numeric nullable data types include a second property that can be used to retrieve the value from a variable as a non-nullable type. This provides the same effect as a cast from a nullable type to its non-nullable counterpart. As with this type of cast however, a run-time error will occur should the value of the variable be null. The previous example can therefore also be written as follows:
int? a = 10;
int? n = null;
int result;
bool checkIfNull;
checkIfNull = a.HasValue; // checkIfNull = true
result = checkIfNull ? a.Value : -1; // result = 10
checkIfNull = n.HasValue; // checkIfNull = false
result = checkIfNull ? n.Value : -1; // result = -1
result = n.Value; // This causes a run-time error.
GetValueOrDefault Method
The GetValueOrDefault method is available to all of the numeric nullable data types. This method provides all of the functionality of the previous example in a single line of code. The method can be called in two ways. If the method is used without a parameter then the numeric value of the nullable data is returned. If the variable in question has a null value, a zero is returned instead. The second manner to call the method includes passing a parameter to specify the default value to replace nulls with. As with all methods, the parameter is held in parentheses with an empty pair of parentheses should no parameter be specified.
int? a = 10;
int? n = null;
int result;
result = a.GetValueOrDefault(); // result = 10
result = n.GetValueOrDefault(); // result = 0
result = n.GetValueOrDefault(-1); // result = -1
The Null Coalescing Operator
The null coalescing operator is a new operator introduced as a part of the .NET framework version 2.0. This operator can be used on any numeric nullable data type and also other nullable data types that have yet to be introduced in the C# Fundamentals tutorial.
The null coalescing operator tests the value of a variable to check if it is null. If the value is not null then the variable's value is returned unaffected. If the variable is null however, a substitute value is provided as a second operand. The operator provides similar functionality to the GetValueOrDefault method with the benefit that it can be used on data that does not provide this functionality. The operator's symbol is a double question mark (??).
int? a = 10;
int? n = null;
int result;
result = a ?? -1; // result = 10
result = n ?? -1; // result = -1
C# Nullable Numeric Data Types
Posted by
Rajesh Rolen
at
Monday, July 27, 2009
Labels: C#.NET
0 comments:
Post a Comment