Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   The CLR Has Special Support for Nullable Value Types

The CLR has built-in support for nullable value types. This special support is provided for boxing, unboxing, calling GetType, calling interface methods, and it is given to nullable types to make them fit more seamlessly into the CLR. This also makes them behave more naturally and as most developers would expect. Let’s take a closer look at the CLR’s special support for nullable types.

 

Boxing Nullable Value Types

Imagine a Nullable<Int32> variable that is logically set to null. If this variable is passed to a method prototyped as expecting an Object, the variable must be boxed, and a reference to the boxed Nullable<Int32> is passed to the method. This is not ideal because the method is now being passed a non-null value even though the Nullable<Int32> variable logically contained the value of null. To fix this, the CLR executes some special code when boxing a nullable variable to keep up the illusion that nullable types are first-class citizens in the environment.

Specifically, when the CLR is boxing a Nullable<T> instance, it checks to see if it is null, and if so, the CLR doesn’t actually box anything, and null is returned. If the nullable instance is not null, the CLR takes the value out of the nullable instance and boxes it. In other words, a Nullable<Int32> with a value of 5 is boxed into a boxed-Int32 with a value of 5. Here is some code that demonstrates this behavior.

 

// Boxing Nullable<T> is null or boxed T Int32? n = null;

Object o = n; // o is null

Console.WriteLine("o is null={0}", o == null); // "True"

 

n = 5;

o = n; // o refers to a boxed Int32

Console.WriteLine("o's type={0}", o.GetType()); // "System.Int32"


Unboxing Nullable Value Types

The CLR allows a boxed value type T to be unboxed into a T or a Nullable<T>. If the reference to the boxed value type is null, and you are unboxing it to a Nullable<T>, the CLR sets Nullable<T>’s value to null. Here is some code to demonstrate this behavior.

 

// Create a boxed Int32 Object o = 5;

 

// Unbox it into a Nullable<Int32> and into an Int32 Int32? a = (Int32?) o; // a = 5

Int32 b = (Int32) o; // b = 5

 

// Create a reference initialized to null o = null;

 

// "Unbox" it into a Nullable<Int32> and into an Int32 a = (Int32?) o; // a = null

b = (Int32) o; // NullReferenceException

 

Calling GetType via a Nullable Value Type

When calling GetType on a Nullable<T> object, the CLR actually lies and returns the type T instead of the type Nullable<T>. Here is some code that demonstrates this behavior.

 

Int32? x = 5;

 

// The following line displays "System.Int32"; not "System.Nullable<Int32>" Console.WriteLine(x.GetType());

 

 

Calling Interface Methods via a Nullable Value Type

In the following code, I’m casting n, a Nullable<Int32>, to IComparable<Int32>, an interface type. However, the Nullable<T> type does not implement the IComparable<Int32> interface as Int32 does. The C# compiler allows this code to compile anyway, and the CLR’s verifier considers this code verifiable to allow you a more convenient syntax.



 

Int32? n = 5;

Int32 result = ((IComparable) n).CompareTo(5); // Compiles & runs OK Console.WriteLine(result); // 0

 

If the CLR didn’t provide this special support, it would be more cumbersome for you to write code to call an interface method on a nullable value type. You’d have to cast the unboxed value type first before casting to the interface to make the call.

 

Int32 result = ((IComparable) (Int32) n).CompareTo(5); // Cumbersome


 
 

 

 


PART IV

Core Facilities

CHAPTER 20..............Exceptions and State Management451

CHAPTER 21....The Managed Heap and Garbage Collection............ 505

CHAPTER 22...................CLR Hosting and AppDomains............ 553

CHAPTER 23................Assembly Loading and Reflection............ 583

CHAPTER 24.............................Runtime Serialization............ 611

CHAPTER 25..........Interoperating with WinRT Components............ 643


C HA P T E R 2 0


Date: 2016-03-03; view: 650


<== previous page | next page ==>
Nbsp;   C#’s Support for Nullable Value Types | Exceptions and State Management
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.009 sec.)