C#
  Home arrow C# arrow Page 3 - 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
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 23
    2005-07-27

    Table of Contents:
  • Behind the Scenes Look at C#: Type Conversions
  • The Value-Type Example
  • The Reference-Type Example
  • Implicit Type Conversions (Built-In Value-Types)
  • Explicit Type Conversions (Built-In Value-Types)
  • The Checked and Unchecked Keywords

  • 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 - The Reference-Type Example


    (Page 3 of 6 )

    In this example I use inheritance to form a hierarchy of C# classes. Inheritance will be discussed in the series shortly; for now just understand that C# permits only single inheritance from a base class and permits multiple inheritance from interfaces. You use the : operator after the class identifier declaration to form the inheritance.

    In this example, the class Person defines two properties with two private fields and a ToString() method. The class Worker inherits from the class Person. It adds the Department property and the private field department in addition to overriding the base class ToString method (in this case the base class here is the Person class). The example addresses the difference in casting between value-types and reference-types.

    using System;

    namespace TypeConversion
    {
    class Class1
    {

    static void Main(string[] args)
    {
    Worker worker1 = new Worker();
    worker1.FirstName = "Mary";
    worker1.LastName = "John";
    worker1.Department = "IT";
    Console.WriteLine("The worker1 object ToString() method prints");
    Console.WriteLine(worker1.ToString());
    Console.WriteLine();

    Person someone = (Person)worker1;
    Console.WriteLine("The cast operator has been used with the worker1 object");
    Console.WriteLine("The someone object ToString() method prints");
    Console.WriteLine(someone.ToString());
    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;
    }
    }
    }

    The result to the console window is:

    As you can see, the result is the same (Mary John, IT) even when we assign the worker1 object to a reference of type Person, but the interesting issue here is when you use the . operator to access the members of each object reference. Let's take a look, when you use the dot operator (.) on the worker1 object you will get the following list:

    As you can see, the Department property is shown with the other public members of the class. But when we cast the worker1 object to a reference of the base class (Person), we have limited our visibility of the object members to the members of the base class. That's what we will see when we use the dot operator (.) with the instance someone:

    The Department is not shown in the list because of the casting operation, but we didn't lose this piece of data. The ToString() method can prove that, because it has printed the same string that contains the IT character value. So with value-types we lose data with casting because it's a copying operation; we copy bits from one type's variable to another.  

    The situation is different with reference-types; we have the object in the managed heap (unlike the value-types, where the instance is stack-based). When we do the cast operation we just limit the visibility of the object to the object that we are casting to (the base class). I think that now you have a clear vision of the whole casting issue, so let's discuss implicit and explicit type conversions with both value-types and reference-types.

    More C# Articles
    More By Michael Youssef


       · This article says that Int32 reserves a bit as a sign bit. This isn't true at all! ...
     

    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 6 hosted by Hostway
    Stay green...Green IT