GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — C# Nullable Types and Null Coalescing Operator
Selected Reading
C# Nullable Types and Null Coalescing OperatorC# provides two special types: nullable types and the null coalescing operator (??). Nullable types allow variables to store null in addition to their normal range of values. Whereas, the null coalescing operator assigns a default value when a nullable type contains null. C# Nullable TypesC# provides a special data types, the nullable types, to which you can assign normal range of values as well as null values. For example, you can store any value from -2,147,483,648 to 2,147,483,647 or null in a Nullable<Int32> variable. Similarly, you can assign true, false, or null in a Nullable<bool> variable. Declaring Nullable TypesYou can declare a nullable type using the ? symbol after the data type. The following is the syntax for declaring a nullable type − < data_type> ? <variable_name> = null; Example of Nullable Data TypesThe following basic example demonstrates the use of nullable data types in C# − using System;
namespace CalculatorApplication {
class NullablesAtShow {
static void Main(string[] args) {
int? num1 = null;
int? num2 = 45;
double? num3 = new double?();
double? num4 = 3.14157;
bool? boolval = new bool?();
// display the values
Console.WriteLine("Nullables at Show: {0}, {1}, {2}, {3}", num1, num2, num3, num4);
Console.WriteLine("A Nullable boolean value: {0}", boolval);
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result − Nullables at Show: , 45, , 3.14157 A Nullable boolean value: C# Null Coalescing Operator (??)The null coalescing operator is used with the nullable value types and reference types. It is used for converting an operand to the type of another nullable (or not) value type operand, where an implicit conversion is possible. If the value of the first operand is null, then the operator returns the value of the second operand, otherwise it returns the value of the first operand. Declaring Using Null Coalescing OperatorYou can use the null coalescing operator (??) to assign a default value when a nullable variable contains The following is the syntax for using the null coalescing operator − <variable_name> = <nullable_value> ?? <default_value>; Example of Null Coalescing OperatorIn the following example, we demonstrates the using System;
namespace CalculatorApplication {
class NullablesAtShow {
static void Main(string[] args) {
double? num1 = null;
double? num2 = 3.14157;
double num3;
num3 = num1 ?? 5.34;
Console.WriteLine(" Value of num3: {0}", num3);
num3 = num2 ?? 5.34;
Console.WriteLine(" Value of num3: {0}", num3);
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result − Value of num3: 5.34 Value of num3: 3.14157 Advertisements
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |