C#
  Home arrow C# arrow Page 5 - Polymorphism in C#
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Polymorphism in C#
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 21
    2007-05-17

    Table of Contents:
  • Polymorphism in C#
  • Versioning with new and override
  • Abstract Classes
  • The Root of All Classes: Object
  • Boxing and Unboxing Types
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Polymorphism in C# - Boxing and Unboxing Types


    (Page 5 of 6 )

    Boxing and unboxing are the processes that enable value types (such as, integers) to be treated as reference types (objects). The value is “boxed” inside an Object and subsequently “unboxed” back to a value type. It is this process that allowed you to call theToString()method on the integer in Example 11-4.

    Boxing Is Implicit

    Boxing is an implicit conversion of a value type to the type Object. Boxing a value allocates an instance ofObjectand copies the value into the new object instance, as shown in Figure 11-4.


    Figure 11-4.  Boxing value types

    Boxing is implicit when you provide a value type where a reference is expected. The runtime notices that you’ve provided a value type and silently boxes it within an object. You can, of course, first cast the value type to a reference type, as in the following:

      int myIntegerValue = 5;
      object myObject = myIntegerValue; // cast to an object
      myObject.ToString();

    This is not necessary, however, as the compiler boxes the value for you silently and with no action on your part:

      int myIntegerValue = 5;
      myIntegerValue.ToString(); // myIntegerValue is boxed

    Unboxing Must Be Explicit

    To return the boxed object back to a value type, you must explicitly unbox it. For the unboxing to succeed, the object being unboxed must really be of the type you indicate when you unbox it.

    You should accomplish unboxing in two steps:

    1. Make sure the object instance is a boxed value of the given value type. 
    2. Copy the value from the instance to the value-type variable.

    Example 11-5 illustrates boxing and unboxing.

    Example 11-5. Boxing and unboxing

    using System;
    public class UnboxingTest
    {
      
    public static void Main()
       {
          int myIntegerVariable = 123;

          //Boxing
          object myObjectVariable = myIntegerVariable;
          Console.WriteLine( "myObjectVariable: {0}",
          myObjectVariable.ToString() );

          // unboxing (must be explicit)
          int anotherIntegerVariable = (int)myObjectVariable;
          Console.WriteLine( "anotherIntegerVariable: {0}",
          anotherIntegerVariable );
       }
    }

    Output:
    myObjectVariable: 123 anotherIntegerVariable: 123

    Figure 11-5 illustrates unboxing.

    Example 11-5 creates an integermyIntegerVariableand implicitly boxes it when it is assigned to the objectmyObjectVariable; then, to exercise the newly boxed object, its value is displayed by callingToString().

    The object is then explicitly unboxed and assigned to a new integer variable,anotherIntegerVariable, whose value is displayed to show that the value has been preserved.

     


    Figure 11-5.  Unboxing

    Avoiding Boxing with Generics

    The most common place that value types were boxed in C# 1.x was in collections that expected Objects. Now that C# supports generics, collections that hold integers need not box and unbox them, and that can increase performance when you have a very large collection. Generics are discussed in more detail in the Chapter 14.

    More C# Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Learning C# 2005, Second Edition,"...
     

    Buy this book now. This article is excerpted from chapter 11 of Learning C# 2005, Second Edition, written by Jesse Liberty and Brian MacDonald (O'Reilly, 2006; ISBN: 0596102097). Check it out today at your favorite bookstore. Buy this book now.

    C# ARTICLES

    - Color Transformation in C# GDI+ Programming
    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway