C#
  Home arrow C# arrow 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


    (Page 1 of 4 )

    We will continue our discussion of type conversion. In this article we will look at implicit and explicit type conversion with reference-types; after that, we will discuss user-defined type conversion and see how we can define our own conversion methods using the keywords implicit or explicit along with the operator keyword. But before we do all that a look at the System.Convert class is in order.

    The System.Convert Class

    The System.Convert class contains methods to convert from any primitive data type to any other primitive data type (except for the object data type because that's a general data type). The class is declared using the keyword sealed, which means that you can't inherit it. All of the class methods are declared as static, so you access the class members using the class name.

    The methods of the System.Convert class are implemented to preserve the culture and to check the ranges (they use the checked block). The methods can throw runtime exceptions like System.InvalidCastException if the conversion operation can't produce a valid value and System.OverflowException when the value exceeds the maximum range of the type that we are converting to, as in the following Main method:

    static void Main(string[] args)
    {
      short x = 300;
      byte y = Convert.ToByte(x);
      Console.WriteLine(y);
      Console.ReadLine();
    }

    The code throws a runtime exception of type System.OverflowException because the value 300 is greater than the maximum range of the byte data type. The convert class contains methods that have overloaded versions to accept all of the primitive data types (except object) as a parameter and return a specific data type. For example, the method Convert.ToInt32 has 19 overloaded versions that accept all of the primitive data types and return an Int32 data type. When you use the methods of the convert class to convert from a numeric data type to another data type, the methods will not throw an exception if there's a loss of little precision digits, but they will throw an exception if the value (after the lose of the little precision digits) is greater than the maximum range of the return type.

    There is another issue you need to know about before we leave this section. Each primitive data type provides a method called Parse(). Parse() accepts a string representation as a parameter, and returns a parsed value of the string that represents the data type that contains the Parse() method. Of course the Parse() method can throw runtime exceptions, and the exceptions differ from one type to another.

    For example, the first line of code uses the Parse() method of the Byte data type, which throws a runtime exception of type System.OverflowException when trying to convert the value 500 to byte data type. The next line of code uses the Parse() method of the DateTime data type to convert the string "Hi Guys" to a DateTime value; it throws a runtime exception of type System.FormatException because the string can't be parsed to represent a DateTime value.

    static void Main(string[] args)
    {
      byte x = Byte.Parse("500");
      DateTime y = DateTime.Parse("Hi Guys");
    }

    To use the parse method you need to be sure that the string value will return the right format for that type when it is parsed. Take a look at the following application:

    using System;
    namespace TypeConversion
    {
      class Class1
      {
        static void Main(string[] args)
        {
          string x = "4000";
          int y = Int32.Parse(x) + 1;
          Console.WriteLine(y); 
          Console.ReadLine();
        }
      }
    }

    When you run this application you will get the value 4001 because the Int32.Parse() method has parsed the string value "4000" and converted it to an Int32 data type.

    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

    - 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#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





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