C# Numeric Types

Description

C# has the predefined numeric types shown in the following table.

C# typeSystem typeSuffixSizeRange
sbyteSByteN/A8 bits-27 to 27 -1
shortInt16N/A16 bits-215 to 215-1
intInt32N/A32 bits-231 to 231-1
longInt64L64 bits-263 to 263-1

Integral?unsigned

C# typeSystem typeSuffixSizeRange
byteByteN/A8 bits0 to 28 -1
ushortUInt16N/A16 bits0 to 216-1
uintUInt32U32 bits0 to 232-1
ulongUInt64UL64 bits0 to 264-1

Real Numbers

C# typeSystem typeSuffixSizeRange
floatSingleF32 bits+/- (~10-45 to 1038)
doubleDoubleD64 bits+/- (~10-324 to 10308)
decimalDecimalM128 bits+/- (~10-28 to 1028)

Note

float and double are called floating-point types and are typically used for scientific calculations.

The decimal type is typically used for financial calculations, where base-10-accurate arithmetic and high precision are required.

Example

The following code prints out the range of integer types in C#.


using System;/*  w w w  .j  a  v a2s . c o  m*/
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Security.Cryptography;

public class MainClass
{
    public static void Main()
    {
        Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
            typeof(byte).ToString(), sizeof(byte), byte.MinValue, byte.MaxValue);
        Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
            typeof(char).ToString(), sizeof(char), (int)char.MinValue, (int)char.MaxValue);
        Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
            typeof(short).ToString(), sizeof(short), short.MinValue, short.MaxValue);
        Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
            typeof(int).ToString(), sizeof(int), int.MinValue, int.MaxValue);
        Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
            typeof(long).ToString(), sizeof(long), long.MinValue, long.MaxValue);
        Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
            typeof(sbyte).ToString(), sizeof(sbyte), sbyte.MinValue, sbyte.MaxValue);
        Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
            typeof(ushort).ToString(), sizeof(ushort), ushort.MinValue, ushort.MaxValue);
        Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
            typeof(uint).ToString(), sizeof(uint), uint.MinValue, uint.MaxValue);
        Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
            typeof(ulong).ToString(), sizeof(ulong), ulong.MinValue, ulong.MaxValue);
    }

}

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