CSharp – Conversions

Type conversion is basically converting one type of data to another type.

Example:


using System;
using System.Collections;

namespace ConsoleApplication1
{
    class StringConversion
    {
        static void Main(string[] args)
        {
            int i = 75;
            float f = 53.005f;
            double d = 2345.7652;
            bool b = true;

            Console.WriteLine(i.ToString());
            Console.WriteLine(f.ToString());
            Console.WriteLine(d.ToString());
            Console.WriteLine(b.ToString());
            Console.ReadKey();

        }
    }
}

The result is:

75
53.005
2345.7652
True

Conversion Methods:

ToBoolean()
Converts a type to a Boolean value, where possible.

ToByte()
Converts a type to a byte.

ToChar()
Converts a type to a single Unicode character, where possible.

ToDateTime()
Converts a type (integer or string type) to date-time structures.

ToDecimal()
Converts a floating point or integer type to a decimal type.

ToDouble()
Converts a type to a double type.

ToInt16()
Converts a type to a 16-bit integer.

ToInt32()
Converts a type to a 32-bit integer.

ToInt64()
Converts a type to a 64-bit integer.

ToSbyte()
Converts a type to a signed byte type.

ToSingle()
Converts a type to a small floating point number.

ToString()
Converts a type to a string.

ToType()
Converts a type to a specified type.

ToUInt16()
Converts a type to an unsigned int type.

ToUInt32()
Converts a type to an unsigned long type.

ToUInt64()
Converts a type to an unsigned big integer.

My Official Website: http://www.lucedigitale.com

Ref: http://www.tutorialspoint.com/csharp/csharp_type_conversion.htm