CSharp.Net-Informations.com
   Home      .Net Framework      VB.NET      C#                                                                      About


  C# type conversions

Conversion is the process of changing the value of one Type to another. System.Convert class provides a complete set of methods for supported conversions.

In CSharp type conversions are divided into two , Implicit Conversions and Explicit Conversions . Conversions declared as implicit occur automatically, when required and Conversions declared as explicit require a cast to be called.

  1:int ctr = 999;
  2:long count = ctr;

// implicit conversion from int type to long type

From the above statements , first line declare an integer type variable ctr and assigned 999 to it. Second line declare a long type variable count and assign the value of ctr to count. Here the conversion occurred automatically. Because we converted an integer type to long type . This type of conversion is called Implicit Conversion .

  1:int ctr = 999;
  2:long count = ctr;
  // implicit conversion from int type to long type
  3:int cnt = (int)count;

// explicit conversion from long type to int type

We already saw the Implicit Conversion happened in the second line . The third line again we converted long Type to an integer type . Here we explicitly convert long type to integer (int cnt = (int)count), otherwise the compiler will show compiler error - Error 1 Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?) . This type of conversion is called Explicit Conversion .

The following C# source code shows how to use System.Convert class.

         C# Source Code Download           Print Source Code
         C# type conversions - Download
        
C# Tutorial

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string str = "true";
                bool flag = System.Convert.ToBoolean(str);
                MessageBox.Show("flg value is " + flag);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot conver the value" + ex.ToString() );
            }
        }
    }
}



CSharp Language Basics Related Contents
*     C# Types
*     C# boxing and unboxing
*     C# DataTypes
*     C# Access Modifiers , CSharp Access Specifiers


   Home      VB.NET      C#
CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
*     C# DataGridView Tutorial
   Home      VB.NET      C#
More Source Code :   
Mail to :  feedback@net-informations.com
  |  Home   |  VB.NET   |  C#   |  SiteMap   |  Terms of Use   |  About   |
net-informations.com (C) 2010
All Rights Reserved. All other trademarks are property of their respective owners.