C# Numeric Types

In this chapter you will learn:

  1. C# Numeric Types
  2. Integral?unsigned
  3. Real Numbers
  4. Note for real numbers in C#
  5. Example for the range of integer 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  . ja  v a2 s .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.

Next chapter...

What you will learn in the next chapter:

  1. Integral literals
  2. Real literals
  3. Numeric literal type inference
Home »
  C# Tutorial »
    C# Language »
      C# Data Types
C# Predefined Type Taxonomy
C# Numeric Types
C# Numeric Literals
C# Numeric suffixes
C# float and double
C# Float and Double Special Values
C# decimal
C# Numeric Conversions
C# Bool Type
C# char type
C# String Type