C#
  Home arrow C# arrow Page 4 - 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
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 - Implicit Type Conversions (Built-In Value-Types)


    (Page 4 of 6 )

    Implicit conversion on types are those conversion operations that the C# compiler performs without warning us. Actually the C# compiler doesn't warn you about implicit conversions because there's no loss of data with those operations and they are save operations. The implicit type operation performs conversion from a small type to a larger one; that's why we don't lose our data. For example, you can assign a variable of type short (which contains the maximum value of this type) to a variable of type int; you will not lose any data because int is bigger than short, and the bits will be copied into the int variable exactly.

    But why do we need implicit conversions? It's a matter of memory management. We have different types to hold different ranges of numbers. We use the small types when our scenario requires small values, and we use a larger types when the stored values are large, to save memory. If we had only one type to represent numbers, we wouldn't need to convert between types. In fact, we have many types that hold different ranges to save memory, and we can use type conversion to convert between types. Take a look at the following example:

    using System;

    namespace TypeConversion
    {
    class Class1
    {

    static void Main(string[] args)
    {
    short x = 9;
    int y = 8;
    int result = x + y; // Implicit conversion occurs here
    Console.WriteLine("the value of the result variable = " + result);
    Console.WriteLine("calling the GetLong method and passing result");
    long i = GetLong(result); // implicit conversion occurs here again
    Console.WriteLine("i = {0}", i);
    Console.ReadLine();
    }

    static long GetLong(long temp)
    {
    return 10 * temp;
    }
    }
    }

    The result is:

    In this simple example the compiler performed implicit conversion operations between types. In the first operation, when we add x to y, the addition operator (+) doesn't work with the short data type, so it converts the short variable to an int data type implicitly. The second conversion operation happens when we pass the result variable to the method GetLong, that accepts a parameter of type long; the Compiler converts the argument result from an int data type to long data type, then passes it to the method implicitly.

    The compiler does this all the time; you don't have to remember that because there's no loss of data. This conversion operation is called asymmetrical operation. This means that you can do this type of conversion, but you can't do the reverse operation implicitly. In other words, you can't assign a value of a large type to a small one implicitly, because you may lose data. You need explicit conversion in order to do this operation.

    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

    - 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek