// Implicitly cast myVar1 to type A. // This cast is fine because the data is of type B. in Visual C#

Maker DataMatrix in Visual C# // Implicitly cast myVar1 to type A. // This cast is fine because the data is of type B.

// Implicitly cast myVar1 to type A. // This cast is fine because the data is of type B.
Paint DataMatrix In C#.NET
Using Barcode maker for VS .NET Control to generate, create DataMatrix image in .NET applications.
www.OnBarcode.com
Recognize DataMatrix In Visual C#.NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Figure 18-21. Casting to a safe type
Making Matrix 2D Barcode In C#
Using Barcode drawer for VS .NET Control to generate, create 2D Barcode image in VS .NET applications.
www.OnBarcode.com
Create Barcode In Visual C#.NET
Using Barcode creator for Visual Studio .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
New page
Denso QR Bar Code Maker In C#
Using Barcode encoder for .NET framework Control to generate, create QR Code ISO/IEC18004 image in .NET applications.
www.OnBarcode.com
Printing UPC-A Supplement 2 In Visual C#
Using Barcode creation for VS .NET Control to generate, create UPC Code image in VS .NET applications.
www.OnBarcode.com
CHAPTER 18 CONVERSIONS
Make Barcode In C#.NET
Using Barcode generator for .NET framework Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Print GTIN - 8 In C#.NET
Using Barcode creation for VS .NET Control to generate, create EAN 8 image in VS .NET applications.
www.OnBarcode.com
Boxing Conversions
Data Matrix ECC200 Drawer In C#.NET
Using Barcode generation for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in .NET framework applications.
www.OnBarcode.com
Data Matrix 2d Barcode Drawer In Java
Using Barcode encoder for Java Control to generate, create Data Matrix ECC200 image in Java applications.
www.OnBarcode.com
All C# types, including the value types, are derived from type object. Value types, however, are efficient, lightweight types that do not, by default, include their object component in the heap. When the object component is needed, however, you can use boxing, which is an implicit conversion that takes a value type value, creates from it a full reference type object in the heap, and returns a reference to the object. For example, Figure 18-22 shows three lines of code. The first two lines of code declare and initialize value type variable i and reference type variable oi. In the third line of code, you want to assign the value of variable i to oi. But oi is a reference type variable, and must be assigned a reference to an object in the heap. Variable i, however, is a value type, and does not have a reference to an object in the heap. The system therefore boxes the value of i by Creating an object of type int in the heap Copying the value of i to the int object Returning the reference of the int object to oi to store as its reference
Encoding UPC Code In None
Using Barcode printer for Online Control to generate, create GS1 - 12 image in Online applications.
www.OnBarcode.com
Code 128A Reader In None
Using Barcode reader for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Figure 18-22. Boxing creates a full reference type object from a value type.
Making GS1 - 12 In VB.NET
Using Barcode printer for Visual Studio .NET Control to generate, create UPC-A Supplement 2 image in Visual Studio .NET applications.
www.OnBarcode.com
EAN / UCC - 14 Encoder In None
Using Barcode maker for Online Control to generate, create GTIN - 128 image in Online applications.
www.OnBarcode.com
New page
Encode Barcode In None
Using Barcode creation for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Painting ANSI/AIM Code 128 In None
Using Barcode printer for Word Control to generate, create Code 128 Code Set C image in Office Word applications.
www.OnBarcode.com
CHAPTER 18 CONVERSIONS
Data Matrix 2d Barcode Creator In VS .NET
Using Barcode creation for VS .NET Control to generate, create Data Matrix image in Visual Studio .NET applications.
www.OnBarcode.com
Recognize QR Code ISO/IEC18004 In Visual Basic .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Boxing Creates a Copy
Barcode Creation In Objective-C
Using Barcode generation for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Print Data Matrix 2d Barcode In Objective-C
Using Barcode creator for iPhone Control to generate, create Data Matrix image in iPhone applications.
www.OnBarcode.com
A common misunderstanding about boxing is that it somehow acts upon the item being boxed. It doesn t. It returns a reference type copy of the value. After the boxing procedure, there are two copies of the value the value type original and the reference type copy each of which can be manipulated separately. For example, the following code shows the separate manipulation of each copy of the value. Figure 18-23 illustrates the code. The first line defines value type variable i and initializes its value to 10. The second line creates reference type variable oi and initializes it with the boxed copy of variable i. The last three lines of code show i and oi being manipulated separately. int i = 10; // Create and initialize value type Box i and assign its reference to oi. object oi = i; // Create and initialize reference type Console.WriteLine("i: {0}, io: {1}", i, oi); i = 12; oi = 15; Console.WriteLine("i: {0}, io: {1}", i, oi); This code produces the following output: i: 10, io: 10 i: 12, io: 15
Figure 18-23. Boxing creates a copy that can be manipulated separately.
New page
CHAPTER 18 CONVERSIONS
The Boxing Conversions
Figure 18-24 shows the boxing conversions. Any value type ValueTypeS can be implicitly converted to any of types object, System.ValueType, or InterfaceT, if ValueTypeS implements InterfaceT.
Figure 18-24. Boxing is the implicit conversion of value types to reference types.
Unboxing Conversions
Unboxing is the process of converting a boxed object back to its value type. Unboxing is an explicit conversion. The system performs the following steps when unboxing a value to ValueTypeT: It checks that the object being unboxed is actually a boxed value of type ValueTypeT. It copies the value of the object to the variable. For example, the following code shows an example of unboxing a value. Value type variable i is boxed and assigned to reference type variable oi. Variable oi is then unboxed, and its value assigned to value type variable j. static void Main() { int i = 10; Box i and assign its reference to oi. object oi = i; Unbox oi and assign its value to j. int j = (int) oi; Console.WriteLine("i: {0}, }
New page
oi: {1},
j: {2}", i,
oi, j);
CHAPTER 18 CONVERSIONS
This code produces the following output:
i: 10,
oi: 10,
j: 10
Attempting to unbox a value to a type other than the original type raises an InvalidCastException exception.
The Unboxing Conversions
Figure 18-25 shows the unboxing conversions.
Figure 18-25. The unboxing conversions
New page
CHAPTER 18 CONVERSIONS
User-Defined Conversions
Besides the standard conversions, you can also define both implicit and explicit conversions for your own classes and structs. The syntax for user-defined conversions is shown following. The syntax is the same for both implicit and explicit conversion declarations, except for the keywords implicit or explicit. The modifiers public and static are required. Required Operator Target Source public static implicit operator TargetType ( SourceType Identifier ) { Implicit or explicit ... return ObjectOfTargetType; } For example, the following shows an example of the syntax of a conversion method that converts an object of type Person to an int. public static implicit operator int(Person p) { return p.Age; }
Copyright © OnBarcode.com . All rights reserved.