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


    (Page 5 of 6 )

    Let's try to run the following code in VS.NET and see what will happen.

    using System;
    namespace TypeConversion
    {
    class Class1
    {

    static void Main(string[] args)
    {
    long x = 100;
    int y = x; // can I write such code?
    decimal i = 3.45m;
    float o = i; // can I write such code?
    }
    }
    }

    This code can't be compiled. It generates the following errors in the TasList window:

    The compiler can't perform an implicit type conversion operation from a larger type to a smaller one because it might lose data. Although in our example the value 100 is in the valid range of both the long and the int data type (and the same with the decimal and float data types), the compiler can't implicitly convert from type long to type int. You need to explicitly use the cast operation to do this operation.

    So conversions from one type to another smaller type (that contains fewer bits) need an explicit conversion operation because it might lose some data if the value of the type is larger than the maximum value range of the type that we convert to. You must use the cast operation to perform the explicit conversion operation. You use the case operator in the same way as in C; you use parentheses and the type that you want to convert to enclosed in the parentheses, then you put them on the left of the variable or the value that you want to convert. Let's modify the above example to use the cast operator.

    using System;
    namespace TypeConversion
    {
    class Class1
    {

    static void Main(string[] args)
    {
    long x = 100;
    int y = (int)x; // this will work
    Console.WriteLine(y);
    decimal i = 3.45m;
    float o = (float)i; // this will work
    Console.WriteLine(o);
    Console.ReadLine();
    }
    }
    }

    Compile and run the example to get the following result, as we expect:

    We have looked at the bits problem in the value-type example and what would happen when you cast a value that exceeds the maximum value of the type we cast to. So I will not discuss it here again. Just be careful when using explicit casting operations, because you might get an unexpected value. If you get a value like this, try to think about the bits and read the value-type example section again. There are two keywords that can be very useful at this point, the checked and unchecked keywords. Let's take a look.

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