- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Unboxing in .NET
Unboxing Printing PDF-417 2d Barcode In Visual Studio .NET Using Barcode creation for ASP.NET Control to generate, create PDF417 image in ASP.NET applications. www.OnBarcode.comBarcode Printer In VS .NET Using Barcode generation for ASP.NET Control to generate, create bar code image in ASP.NET applications. www.OnBarcode.comBecause a variable of type object can refer to a boxed copy of a value, it s only reasonable to allow you to get at that boxed value through the variable. You might expect to be able to access the boxed int value that a variable o refers to by using a simple assignment statement such as this: PDF-417 2d Barcode Drawer In C# Using Barcode creator for VS .NET Control to generate, create PDF-417 2d barcode image in .NET framework applications. www.OnBarcode.comPDF417 Generation In Visual Studio .NET Using Barcode encoder for .NET Control to generate, create PDF417 image in Visual Studio .NET applications. www.OnBarcode.comint i = o; PDF417 Creator In Visual Basic .NET Using Barcode printer for Visual Studio .NET Control to generate, create PDF-417 2d barcode image in .NET framework applications. www.OnBarcode.comCreating UCC - 12 In VS .NET Using Barcode generation for ASP.NET Control to generate, create USS-128 image in ASP.NET applications. www.OnBarcode.comPart II
Encoding EAN / UCC - 13 In .NET Framework Using Barcode generator for ASP.NET Control to generate, create EAN-13 image in ASP.NET applications. www.OnBarcode.comBarcode Creator In .NET Using Barcode printer for ASP.NET Control to generate, create barcode image in ASP.NET applications. www.OnBarcode.comUnderstanding the C# Language
Paint Data Matrix ECC200 In VS .NET Using Barcode maker for ASP.NET Control to generate, create Data Matrix 2d barcode image in ASP.NET applications. www.OnBarcode.comPainting GTIN - 12 In .NET Using Barcode creator for ASP.NET Control to generate, create UPC-A Supplement 2 image in ASP.NET applications. www.OnBarcode.comHowever, if you try this syntax, you ll get a compile-time error. If you think about it, it s pretty sensible that you can t use the int i = o; syntax. After all, o could be referencing absolutely anything and not just an int. Consider what would happen in the following code if this statement were allowed: ANSI/AIM Code 128 Maker In .NET Framework Using Barcode generator for ASP.NET Control to generate, create Code 128A image in ASP.NET applications. www.OnBarcode.comMSI Plessey Drawer In .NET Using Barcode creation for ASP.NET Control to generate, create MSI Plessey image in ASP.NET applications. www.OnBarcode.comCircle c = new Circle(); int i = 42; object o; o = c; i = o; // o refers to a circle // what is stored in i Generate Barcode In None Using Barcode printer for Software Control to generate, create barcode image in Software applications. www.OnBarcode.comUPC Code Generator In VS .NET Using Barcode generation for .NET framework Control to generate, create UPC-A Supplement 2 image in VS .NET applications. www.OnBarcode.comTo obtain the value of the boxed copy, you must use what is known as a cast, an operation that checks whether it is safe to convert one type to another and then does the conversion. You pre x the object variable with the name of the type in parentheses, as in this example: Reading GS1 - 13 In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comDrawing Bar Code In VS .NET Using Barcode maker for Reporting Service Control to generate, create bar code image in Reporting Service applications. www.OnBarcode.comint i = 42; object o = i; i = (int)o; // boxes // compiles okay
DataBar Drawer In Java Using Barcode generator for Java Control to generate, create GS1 DataBar Expanded image in Java applications. www.OnBarcode.comEncoding Data Matrix 2d Barcode In None Using Barcode generation for Font Control to generate, create Data Matrix ECC200 image in Font applications. www.OnBarcode.comThe effect of this cast is subtle. The compiler notices that you ve speci ed the type int in the cast. Next, the compiler generates code to check what o actually refers to at run time. It could be absolutely anything. Just because your cast says o refers to an int, that doesn t mean it actually does. If o really does refer to a boxed int and everything matches, the cast succeeds and the compiler-generated code extracts the value from the boxed int. (In this example, the boxed value is then stored in i.) This is called unboxing. The following diagram shows what is happening: Bar Code Decoder In VB.NET Using Barcode Control SDK for VS .NET Control to generate, create, read, scan barcode image in .NET applications. www.OnBarcode.comData Matrix ECC200 Generator In Objective-C Using Barcode drawer for iPhone Control to generate, create Data Matrix image in iPhone applications. www.OnBarcode.comHowever, if o does not refer to a boxed int, there is a type mismatch, causing the cast to fail. The compiler-generated code throws an InvalidCastException at run time. Here s an example of an unboxing cast that fails: Circle c = new Circle(42); object o = c; // doesn t box because Circle is a reference variable int i = (int)o; // compiles okay but throws an exception at run time 8
Understanding Values and References
You will use boxing and unboxing in later exercises. Keep in mind that boxing and unboxing are expensive operations because of the amount of checking required and the need to allocate additional heap memory. Boxing has its uses, but injudicious use can severely impair the performance of a program. You will see an alternative to boxing in 18, Introducing Generics. Casting Data Safely
By using a cast, you can specify that, in your opinion, the data referenced by an object has a speci c type and that it is safe to reference the object by using that type. The key phrase here is in your opinion. The C# compiler will trust you when it builds your application, but the runtime is more suspicious and will actually check that this is the case when your application runs. If the type of object in memory does not match the cast, the runtime will throw an InvalidCastException, as described in the preceding section. You should be prepared to catch this exception and handle it appropriately if it occurs. However, catching an exception and attempting to recover in the event that the type of an object is not what you expected it to be is a rather cumbersome approach. C# provides two more very useful operators that can help you perform casting in a much more elegant manner, the is and as operators.
|
|