C#
  Home arrow C# arrow Page 2 - Behind the Scenes Look at C#: Type Convers...
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 
Mobile Linux 
App Generation ROI 
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#

Behind the Scenes Look at C#: Type Conversions continued
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2005-08-03

    Table of Contents:
  • Behind the Scenes Look at C#: Type Conversions continued
  • Implicit and Explicit Reference-Types Conversion
  • User Defined Type Conversions
  • Explanation and One More Example

  • 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


    Behind the Scenes Look at C#: Type Conversions continued - Implicit and Explicit Reference-Types Conversion


    (Page 2 of 4 )

    In this section we will discuss the implicit and explicit reference-type conversions in the same class inheritance hierarchy. We are not talking about implicit and explicit user defined type conversions between different classes in different class inheritance hierarchies. The C# compiler knows what to do when you perform explicit type conversion with classes in the same inheritance hierarchy (as we have seen in the previous article in the section "Introduction to Type Conversion, The Reference-Type Example"). The C# compiler also knows what to do when it needs to perform implicit type conversion with classes in the same inheritance hierarchy -- but it doesn't know how to perform implicit and explicit type conversions with classes in different class inheritance hierarchies. That's why C# features a syntax to do this job, as we will see later in this article. For now we will discuss implicit conversions in the same inheritance hierarchy.

    Although implicit and explicit type conversions in the same inheritance hierarchy are related to the concepts of inheritance, I think that this is the best place to discuss it, because it's more related to casting and how object references are related to objects in the runtime.

    If you remember the example of the section "Introduction to Type Conversion, The Reference-Type Example," you will recall that we had an object of type Worker (called worker1) and we cast the reference worker1 to a reference of type Person. In the next application you will understand the whole implicit and explicit reference-type conversion story. Let's take a look at the code first:

    using System;
    namespace TypeConversion
    {
      class Class1
      {

        static void Main(string[] args)
        { 
          Worker worker1 = new Worker(); 
          worker1.FirstName = "Michael";
          worker1.LastName = "Youssef";
          worker1.Department = "IT";
          Console.WriteLine(worker1);
          Console.WriteLine();
          Person aPerson = worker1; // this doesn't require a cast
          Console.WriteLine("this is aPerson = {0}", aPerson);
          Worker worker2 = (Worker)aPerson; // this requires a cast operation 
          Console.WriteLine("this is worker2 = {0}", worker2);
          Console.ReadLine();
        }
      }

      class Person
      {
        private string firstName;
        private string lastName;

        public string FirstName
        {
          get
          { 
            return this.firstName;
          }

          set
          {
            this.firstName = value;
          } 
        }

        public string LastName
        {
          get
          {
            return this.lastName;
          }

          set
          {
            this.lastName = value;
          } 
        }

        public override string ToString()
        {
          return this.firstName +" "+ this.lastName;
        } 

      }

      class Worker : Person
      {
        private string department;

        public string Department
        {
          get
          {
            return this.department; 
          }

          set
          {
            this.department = value;
          }
        }

        public override string ToString()
        {
          return this.FirstName +" "+ this.LastName+","+this.department;
        }
      }
    }

    Run the application after you compile it and you will get the following result to the console window.

    Let's review the implicit and explicit operations involved when converting from one value-type to another. Implicit conversion means that there's no loss of data; the explicit conversion is needed because there might be some loss of data. The case of reference-types is different. As you can see, the object is heap-based. The implicit and explicit cast operations are performed on the object reference itself, and the result of running the application proves that because the same object values are printed to the console window using the object ToString() method.

    We begin the Main method with a declaration of an object reference of type Worker and then assign values to the FirstName, LastName and Department properties. Then, we call the ToString method to print these values to the console window. After that we declare an object reference of type Person (aPerson) and assign the worker1 to it, which assigns the object worker1 to the aPerson reference.

    Actually I did use the cast operator in the first part of the article, only to illustrate that casting is needed when you assign a value to another type that stores a range of values that is less than the value's type. Now you understand that with the reference type it's all about the reference object itself (unlike the value types, which simply hold the values on the stack).

    So the statement Person aPerson = worker1; doesn't need casting, because here there's no lose of data (because it's heap-based). However, we need an explicit cast operation when we use this statement: Worker worker2 = (Worker)aPerson;. Because we know that aPerson refers to an instance of type Worker, we can cast to type Worker with no problems; again, the object is heap-based and we are dealing only with the object reference.

    If the object reference aPerson refers to an object of type Person, then the statement Worker worker2 = (Worker)aPerson; will fail. It will generate the exception System.InvalidCastException because a Worker object contains more members than a Person object. We can't cast that way, as we get a exception of type System.OverflowException when we assign a value that exceeds the maximum range for the value type (when runs it a checked block).

    So with reference types there are down-cast and up-cast conversion operations on object references. When you cast an object reference of a derived class type to an object reference of a base class type, then it's called up-cast, and as you know it doesn't need explicit conversion. The down-cast operation works the other way; when you cast an object reference of a base class to an object reference of a derived class, the operation is called a down-cast.

    More C# Articles
    More By Michael Youssef


       · what happened id i want to convert char type to int typein C# then what have to be...
     

    C# ARTICLES

    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT