Nullable types and ?? operator
This time, i would like to discuss about a cool feature of C#
2.0. As you know, some database operations return null values and results into
throwing exceptions unless you handled well in your business logic. .Net 2.0 has
been solved by introducing nullable types. Lets discuss about its features and
functionalities.
Nullable type can represent all the values of its underlying type, plus the value null. Thus, if you define a nullable boolean, its values comes from either true or false as well as Null.
For, nullable integer can be assigned from integer values and null. We can define a nullable type using its underlying datatype suffixed by a question mark symbol(?). Lets look at the examples
Nullable type can represent all the values of its underlying type, plus the value null. Thus, if you define a nullable boolean, its values comes from either true or false as well as Null.
For, nullable integer can be assigned from integer values and null. We can define a nullable type using its underlying datatype suffixed by a question mark symbol(?). Lets look at the examples
Examples of nullable types
//Nullable values must be assigned with an initial value.
//Nullable values must be assigned with an initial value.
int? intNullable = 2;
double? dblNullable = 37.73;
bool? bNullable = true;
Int[]? arNullable = new int?[1,2];
double? dblNullable = 37.73;
bool? bNullable = true;
Int[]? arNullable = new int?[1,2];
Keep in mind that nullable types are applicable only to value
types or an array of value types.Referance types can't assinged to nullable types,
since nullable types instances of System.Nullable(T) struct(here T is the type). If u define a nullable string, it would result
into compile-time error!!!!..
Properties and Methods
Nullable type has a property called HasValue, which determines whether
the value contains null or not. Nullable types are very useful where you are
interacting with databases, especially columns with null or empty values. There
is very useful method associated with the Nullable types, GetValueOrDefault()
which returns the value of the variable or default value in case of null(for eg:
false for bool, 0 for integer, 0.0 for double).
?? Operator
Lets assume, you are accessing a table which has some columns with
empty(undefined) values or null values. But still you want to fetch the data
since the whole data is very large and time-consuming. Instead of showing those
null values, it could be possible to display some predefined values to end user,
there by making the data more understanding and readable. This feature can be
achieved by using "??" operator. It simply allows you to assign a value to
nullable type if the retrieved value is in fact null. The following code-snippet
makes this feature more clear.
//Suppose you are executing a database
operation
//which returns a null value instead of
integer
public Int
GetMinimumCount()
{
int? iTemp = ConfigManager.GetMinimumCount() ?? 100;
return iTemp.Value;
}
Nullable types are very useful when we are following ORM architecture(eg:
nHibernate), where we would be dealing table fields are class members, so that
the chance of getting null values very high compared to 3-Tier architecture. I
hope you would get an idea about nullable types and its features.