Content:
• Introduction
• System requirement
• Nullable type’s use
• Creating Nullable types
• The ?? Operator
• Sending Nullable types to the Database
• Nullable objects and the relational and logical operators
Introduction:
Prior to C# 2.0 there was an inability to assign a null value to certain types e.g. Int, DateTime, Float, Decimal. Microsoft C# 2.0 provides nullable type that allows you to assign a null to "value types" that have previously not allowed such an assignment. This article provides a walkthrough how to use nullable types in c# and working of ?? Operator with nullable types.
System requirement
The following software required to be installed
• .net framework & Visual studio 2005
Nullable type’s Use:
Databases use null as being distinct from values such as 0 and false. Without nullable types, mapping of c# data type to database null field depends on arbitrary decision taken by user. In different situations a null in a Boolean field may be equivalent to false or it may be equally valid to map it to true. Nullable types overcome this problem and allow any database value - including the “no value” of null - to be mapped directly to value types in C#. Databases are not the only example either. Any situation where something may not be defined, and only has a meaningful value if it is defined is benefit of nullable types. Optional parameters in XML or HTML also can be set equivalent to null which was not directly possible earlier. Likewise the square root of -1 in real number scenarios, the age of a customer who declined to say how old they are, or the time to completion of some batch process that’s not yet started are some perfect uses of nullable types. So Nullable types can save development time and ease program maintenance. In the past, handling the possibility of unused fields required the use of either placeholder values but this could only work if there was a value that would otherwise be invalid or an extra field that simply indicated whether a field was in use, but having to manually create and manage such a field is annoyance. The nullable type solves both the problems.
Creating Nullable types:
A nullable type is a special version of a value type that is represented by a structure. Nullable types are objects of System. Nullable, where T must be a non nullable type. Nullabe types can be declared in two different ways. First it can be explicitly declared as object of System.Nullable. For example, this creates int and bool nullable types –
System.Nullable intCount;
System.Nullable boolDone;
Secondly it can be declared using short hand method as follows:
int? intCount;
bool? boolDone;
There are two ways to determine if a variable of nullable type is null or contains a value. Nullable variable can be compared with null value or HasValue read-only property can be used.
int? intCount = null;
if (intCount != null )
Console.WriteLine(intCount);
if (intCount.HasValue)
Console.WriteLine(intCount);
If a nullable variable is null and its value is tried to obtain then System.InvalidOperationException will be thrown. Nullable variable’s value can be obtained by using the Value read-only property or by casting it into its underlying type.
Following code sample demonstrates this:-
using System;
using System.Collections.Generic;
using System.Text;
namespace NullableTypeBOK
{
class NullableType
{
static void Main(string[] args)
{
int? intCount = null;
if (intCount.HasValue)
Console.WriteLine(intCount.Value);
else
Console.WriteLine("Count has no value");
intCount = 100;
if (intCount.HasValue)
Console.WriteLine(intCount.Value);
else
Console.WriteLine("Count has no value");
Console.Read();
}
}
}
A nullable object can be used in expressions in the same way as its underlying type. When non-nullable and nullable types are mixed in an operation, the outcome is a nullable value.
The ?? Operator:
The ?? Operator is also called the null coalescing operator, which lets us specify a default value that will be used when nullable object contains null. If without using ?? Operator nullable object is tried to cast to its underlying type then System.InvalidOperationException will be thrown if the nullable object contains a null value. It can be declared as follows:-
Nullable-object ?? Default-value
If nullable object contains a value, then the value of ?? Operator is that value. Otherwise the value of the ?? Operator is default value. For example, in the following code amount is null. This causes Interest to be assigned the value 0.0 and no exception will be thrown and if amount is set to some value then interest will be accordingly.
static void Main(string[] args)
{
float Interest;
System.Nullable pAmount = null;
// if pAmount=10.0F then Interest=40 otherwise 0
System.Nullable rate = 0.8F;
System.Nullable time = 5;
Interest = (pAmount * rate * time) ?? 0.0F;
Console.WriteLine(Interest);
Console.Read();
}
The right hand expression of ?? Operator is evaluated only if left hand side of the operator does not contain a value.
Sending Nullable types to the Database:
Nullable types can be sent to the database and have DBNull inserted for types that have a null value. To do this the ??Operator is used and DBNull is casted to an object. The cast is required because the objects on either side of the ??Operator must be compatible.
Nullable objects and the relational and logical operators:
Nullable objects can be used in relational expressions as same as their corresponding Non-Nullable types. However, when two nullable objects are compared using the <, >, <= or >= operators, the result is false if either of the object is null.
When a logical expression involves two bool? Objects, outcome will be true, false or null. Following truth table shows the entries for the & and operators that apply to bool?.
References:
http://www.blogger.com/www.msdn.com
http://www.blogger.com/www.techrepublic.com
• Introduction
• System requirement
• Nullable type’s use
• Creating Nullable types
• The ?? Operator
• Sending Nullable types to the Database
• Nullable objects and the relational and logical operators
Introduction:
Prior to C# 2.0 there was an inability to assign a null value to certain types e.g. Int, DateTime, Float, Decimal. Microsoft C# 2.0 provides nullable type that allows you to assign a null to "value types" that have previously not allowed such an assignment. This article provides a walkthrough how to use nullable types in c# and working of ?? Operator with nullable types.
System requirement
The following software required to be installed
• .net framework & Visual studio 2005
Nullable type’s Use:
Databases use null as being distinct from values such as 0 and false. Without nullable types, mapping of c# data type to database null field depends on arbitrary decision taken by user. In different situations a null in a Boolean field may be equivalent to false or it may be equally valid to map it to true. Nullable types overcome this problem and allow any database value - including the “no value” of null - to be mapped directly to value types in C#. Databases are not the only example either. Any situation where something may not be defined, and only has a meaningful value if it is defined is benefit of nullable types. Optional parameters in XML or HTML also can be set equivalent to null which was not directly possible earlier. Likewise the square root of -1 in real number scenarios, the age of a customer who declined to say how old they are, or the time to completion of some batch process that’s not yet started are some perfect uses of nullable types. So Nullable types can save development time and ease program maintenance. In the past, handling the possibility of unused fields required the use of either placeholder values but this could only work if there was a value that would otherwise be invalid or an extra field that simply indicated whether a field was in use, but having to manually create and manage such a field is annoyance. The nullable type solves both the problems.
Creating Nullable types:
A nullable type is a special version of a value type that is represented by a structure. Nullable types are objects of System. Nullable
System.Nullable
System.Nullable
Secondly it can be declared using short hand method as follows:
int? intCount;
bool? boolDone;
There are two ways to determine if a variable of nullable type is null or contains a value. Nullable variable can be compared with null value or HasValue read-only property can be used.
int? intCount = null;
if (intCount != null )
Console.WriteLine(intCount);
if (intCount.HasValue)
Console.WriteLine(intCount);
If a nullable variable is null and its value is tried to obtain then System.InvalidOperationException will be thrown. Nullable variable’s value can be obtained by using the Value read-only property or by casting it into its underlying type.
Following code sample demonstrates this:-
using System;
using System.Collections.Generic;
using System.Text;
namespace NullableTypeBOK
{
class NullableType
{
static void Main(string[] args)
{
int? intCount = null;
if (intCount.HasValue)
Console.WriteLine(intCount.Value);
else
Console.WriteLine("Count has no value");
intCount = 100;
if (intCount.HasValue)
Console.WriteLine(intCount.Value);
else
Console.WriteLine("Count has no value");
Console.Read();
}
}
}
A nullable object can be used in expressions in the same way as its underlying type. When non-nullable and nullable types are mixed in an operation, the outcome is a nullable value.
The ?? Operator:
The ?? Operator is also called the null coalescing operator, which lets us specify a default value that will be used when nullable object contains null. If without using ?? Operator nullable object is tried to cast to its underlying type then System.InvalidOperationException will be thrown if the nullable object contains a null value. It can be declared as follows:-
Nullable-object ?? Default-value
If nullable object contains a value, then the value of ?? Operator is that value. Otherwise the value of the ?? Operator is default value. For example, in the following code amount is null. This causes Interest to be assigned the value 0.0 and no exception will be thrown and if amount is set to some value then interest will be accordingly.
static void Main(string[] args)
{
float Interest;
System.Nullable
// if pAmount=10.0F then Interest=40 otherwise 0
System.Nullable
System.Nullable
Interest = (pAmount * rate * time) ?? 0.0F;
Console.WriteLine(Interest);
Console.Read();
}
The right hand expression of ?? Operator is evaluated only if left hand side of the operator does not contain a value.
Sending Nullable types to the Database:
Nullable types can be sent to the database and have DBNull inserted for types that have a null value. To do this the ??Operator is used and DBNull is casted to an object. The cast is required because the objects on either side of the ??Operator must be compatible.
Nullable objects and the relational and logical operators:
Nullable objects can be used in relational expressions as same as their corresponding Non-Nullable types. However, when two nullable objects are compared using the <, >, <= or >= operators, the result is false if either of the object is null.
When a logical expression involves two bool? Objects, outcome will be true, false or null. Following truth table shows the entries for the & and operators that apply to bool?.
References:
http://www.blogger.com/www.msdn.com
http://www.blogger.com/www.techrepublic.com
No comments:
Post a Comment