Convert various data types to long in CSharp

Description

The following code shows how to convert various data types to long.

Example


  //www  .j a  v  a  2  s. c om
  
  
    

using System;

class Sample 
{
    public static void Main() 
    {
        bool    xBool = false;
        short   xShort = 1;
        int     xInt   = 2;
        long    xLong  = 3;
        float   xSingle = 4.0f;
        double  xDouble = 5.0;
        decimal xDecimal = 6.0m;
        string  xString = "7";
        char    xChar   = '8'; 
        byte    xByte  =  9;
    
        ushort  xUshort = 120;   
        uint    xUint =   121;
        ulong   xUlong =  122;
        sbyte   xSbyte  = 123;
    
        Console.WriteLine("Boolean:  {0}", Convert.ToInt64(xBool));
        Console.WriteLine("Int16:    {0}", Convert.ToInt64(xShort));
        Console.WriteLine("Int32:    {0}", Convert.ToInt64(xInt));
        Console.WriteLine("Int64:    {0}", Convert.ToInt64(xLong));
        Console.WriteLine("Single:   {0}", Convert.ToInt64(xSingle));
        Console.WriteLine("Double:   {0}", Convert.ToInt64(xDouble));
        Console.WriteLine("Decimal:  {0}", Convert.ToInt64(xDecimal));
        Console.WriteLine("String:   {0}", Convert.ToInt64(xString));
        Console.WriteLine("Char:     {0}", Convert.ToInt64(xChar));
        Console.WriteLine("Byte:     {0}", Convert.ToInt64(xByte));
        Console.WriteLine("UInt16:   {0}", Convert.ToInt64(xUshort));
        Console.WriteLine("UInt32:   {0}", Convert.ToInt64(xUint));
        Console.WriteLine("UInt64:   {0}", Convert.ToInt64(xUlong));
        Console.WriteLine("SByte:    {0}", Convert.ToInt64(xSbyte));
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var