C# Integer Variable Types

C# Integer Variable Types

An Integer data types are used to work with numbers. Perhaps the most widely used of the variable types is the integer.

C# provides a number of different integer types based on number size and whether the integers are signed (positive or negative) or unsigned (positive only). All the integer variable types have one thing in common and that is that they may only be used to store whole numbers.

In C#, the datatype is denoted by the Int32 keyword. Below is an example of how this datatype can be used. In our example, we will define an Int32 variable called numo. We will then assign an Integer value to the variable and then display it accordingly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32 numo = 120;
            Console.WriteLine(numo);
            Console.ReadLine();
        }
    }
}

Output:

If the above code is entered properly and the program is executed successfully, following output will be displayed:

C# Integer Variable Types

Code Explanation:

  • The Int32 data type is specified to declare an Integer variable called numo
  • The variable is then assigned a value of 120.
  • Finally the Console.WriteLine function is used to display the number to the console.

From the output, you can clearly see that the Integer variable called numo was displayed in the console.

We can specify Integer Literals in the ways:

  • Decimal literals (Base 10): In the decimal type of literals 0-9 digits are allowed. No prefix is required for the decimal type of literals.
int x = 200; // decimal type

The following example shows the integer literals in decimal without any prefix:

int quantity = 20;
int amount = 30;

If a number is big, you can use the digit separator (_) to make it more readable. Note that you can use the digit separator (_) for all integer literals, not just decimal. For example:

int prize = 5_000_000;
  • Octal Literals (Base 8): In the octal type of literals 0-7 digits are allowed. 0 is used as a prefix to specify the form of octal type literals.
//The octal number should be prefix with 0.
int x = 072;  // octal type
  • Hexa-decimal literals (Base 16): In the hexadecimal type of literals, digits from 0- 9 and characters from a-f are allowed. Uppercase and lowercase both types of characters are allowed in this case. As we know that c# is a case-sensitive programming language but here c# is not case-sensitive. 0X or 0x is used as a prefix to specify the form of hexadecimal type of literals.
// The hexa-decimal number should be prefix
 with 0X or 0x.
int x = 0X123Fact; // hexadecimal  type   
  • Binary literals (Base 2): In this form, the allowed digits are only 1’s and 0’s. Binary numbers have the 0b or 0B prefix.

int x = 0b101; // The binary number should be prefix with 0b.

int flag = 0b10011110;

Also, you can use the digit separator (_) to separate the digits like this:

int flag = 0b_1001_1110;

 

Integer Types

Integer type numbers are positive or negative whole numbers without decimal points. C# includes four data types for integer numbers: byte, short, int, and long.

01. Byte

The byte data type stores numbers from 0 to 255. It occupies 8-bit in the memory. The byte keyword is an alias of the Byte struct in .NET.

The sbyte is the same as byte, but it can store negative numbers from -128 to 127. The sbyte keyword is an alias for SByte struct in .NET.

Examples: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Byte Max Value is: "+ Byte.MaxValue);
            Console.WriteLine("Byte Min Value is: " + Byte.MinValue);
            Console.WriteLine("SByte Max Value is: " + SByte.MaxValue);
            Console.WriteLine("SByte Min Value is: " + SByte.MinValue);
            Console.ReadLine();
        }
    }
}

Output:

C# Byte Example
02. Short

The short data type is a signed integer that can store numbers from -32,768 to 32,767. It occupies 16-bit memory. The short keyword is an alias for Int16 struct in .NET.

The ushort data type is an unsigned integer. It can store only positive numbers from 0 to 65,535. The ushort keyword is an alias for UInt16 struct in .NET.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("short(Int16) Max Value is: " + Int16.MaxValue);
            Console.WriteLine("short(Int16) Min Value is: " + Int16.MinValue);
            Console.WriteLine("ushort(Unt16) Max Value is: " + UInt16.MaxValue);
            Console.WriteLine("ushort(Unt16) Min Value is: " + UInt16.MinValue);
            Console.ReadLine();
        }
    }
}

Output:

C# Short Example
03. Int

The int data type is 32-bit signed integer. It can store numbers from -2,147,483,648 to 2,147,483,647. The int keyword is an alias of Int32 struct in .NET.

The uint is 32-bit unsigned integer. The uint keyword is an alias of UInt32 struct in .NET. It can store positive numbers from 0 to 4,294,967,295. Optionally use U or u suffix after a number to assign it to uint variable.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("int(Int32) Max Value is: " + Int32.MaxValue);
            Console.WriteLine("int(Int32) Min Value is: " + Int32.MinValue);
            Console.WriteLine("uint(UInt32) Max Value is: " + UInt32.MaxValue);
            Console.WriteLine("uint(UInt32) Min Value is: " + UInt32.MinValue);
            Console.ReadLine();
        }
    }
}

Output:

C# Int Example
04. Long

The long type is 64-bit signed integers. It can store numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use l or L suffix with number to assign it to long type variable. The long keyword is an alias of Int64 struct in .NET.

The ulong type stores positive numbers from 0 to 18,446,744,073,709,551,615. If a number is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, its type is ulong. The ulong keyword is an alias of UInt64 struct in .NET.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("long(Int64) Max Value is: " + Int64.MaxValue);
            Console.WriteLine("long(Int64) Min Value is: " + Int64.MinValue);
            Console.WriteLine("ulong(UInt64) Max Value is: " + UInt64.MaxValue);
            Console.WriteLine("ulong(UInt64) Min Value is: " + UInt64.MinValue);
            Console.ReadLine();
        }
    }
}

Output:

C# Long Example

C# Min and max values

Each integer type has the MinValue and MaxValue constants that provide the minimum and maximum of the type.

To access these values, you use the dot operator (.). 

For example:

int.MinValue
int.MaxValue

The following displays the range of the int type:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"int range: ({int.MinValue},{int.MaxValue})");
            Console.ReadLine();
        }
    }
}

Output:

C# Min and max values

Various C# integer variable types

The following table lists the various C# integer variable types together with details of the number of bytes of physical memory consumed by each type and the acceptable value ranges:

TypeSize in BytesValue Range
byte1 byte0 to 255
sbyte1 byte-128 to 127
short2 byte-32,768 to 32,767
ushort2 byte0 to 65,535
int4 byte-2,147,483,648 to 2,147,483,648
uint4 byte0 to 4,294,967,295
long8 byte-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong8 byte0 to 18,446,744,073,709,551,615