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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 15
    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 - User Defined Type Conversions


    (Page 3 of 4 )

    When the C# compiler needs to perform an implicit conversion on primitive data types, it knows exactly what it needs and the operations it will perform; it also knows what to do with the explicit cast operations. The same thing applies to the classes that exist in the same class inheritance hierarchy; the C# compiler knows what to do when it needs to perform an implicit or an explicit conversion operation. What if we need to convert from a type to another one that doesn't exist in the same class inheritance hierarchy? C# offers a syntax that's similar to operator overloading that can be used when you need to convert (implicitly or explicitly) from one type to another.

    The syntax that C# offers for user defined type conversions can be used to state an implicit type conversion between two classes (or structures) or an explicit type conversion. Of course we have to simulate the compiler's behavior. In other words, we use the cast operator when we convert a double variable to an int because we know that we may lose some data. The C# compiler performs an implicit type conversion when the operation is saved, and it will not lose any data.

    The same point applies to user defined conversions. When we are sure that the conversion operation from one type to another type is saved, we will use the syntax that defines an implicit conversion. If the operation can cause some data to be lost, however, we will use the syntax of the explicit conversion (for which the cast operator must be used) to state that. The user defined type conversions can be defined and used with classes as well as structures, and the syntax is the same. Note that you can use the C# user defined type conversion with classes in the same class  inheritance hierarchy, but not between a derived type and a base type; you can use it between two classes that participate in the same base class.

    For example, you have the classes Customer and Worker, which both inherit from the class Person. In this class  inheritance hierarchy you can't define user defined type conversions between the Customer class and its base class (Person); you also can't perform the operation between the Worker class and its base class (Person) because they are in the same inheritance hierarchy, and the compiler can perform implicit and explicit casting operations. The only valid user defined type conversion here is between the Customer class and the Worker class. Let's see that in an example:

    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();

          Customer customer1 = new Customer();
          customer1.FirstName = "Mary";
          customer1.LastName = "Jerry";
          customer1.OrderID = "123456789";
          Console.WriteLine(customer1);
          Console.WriteLine();

          Console.WriteLine("casting the worker1 object to customer1");
          customer1 = (Customer)worker1;
          Console.WriteLine();
          Console.WriteLine("the object customer1 after the casting");
          Console.WriteLine();
          Console.WriteLine(customer1);
          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;
          } 
        }
      }

      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;
        }

        public static explicit operator Customer(Worker work)
        {
          Customer temp = new Customer();
          temp.FirstName = work.FirstName;
          temp.LastName = work.LastName;
          temp.OrderID = "000000000";
          return temp;
        }
      }

      class Customer: Person
      {
        private string orderID;

        public string OrderID
        {
          get
          {
            return this.orderID;
          }

          set
          {
            this.orderID = value;
          }
        }

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

    Compile the code and run the application and you will get the following result to the console window:

    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

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - 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#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek