C# Int16, Int32, and Int64 Types

You want to see what the Int16Int32, and Int64 types in the C# language are equivalent to. Typically, C# programmers prefer the C-style numeric types, which are easier to read. In this example program, we demonstrate that the Int16, Int32, and Int64 types are equivalent to more commonly used types.

Int16 -> short
Int32 -> int  
Int64 -> long

Example

This simple program simply declares Int16, Int32, and Int64 variable instances, and then prints the Type object corresponding to them in the runtime. Then, it does the same exact thing for the short, int, and long types. The program’s output shows that Int16 is equal to short, Int32 is equal to int, and Int64 is equal to long in the runtime.

Program that uses Int16, Int32, Int64 types [C#]

using System;

class Program
{
    static void Main()
    {
	{
	    Int16 a = 1;
	    Int32 b = 1;
	    Int64 c = 1;

	    Console.WriteLine(a.GetType());
	    Console.WriteLine(b.GetType());
	    Console.WriteLine(c.GetType());
	}
	{
	    short a = 1;
	    int b = 1;
	    long c = 1;

	    Console.WriteLine(a.GetType());
	    Console.WriteLine(b.GetType());
	    Console.WriteLine(c.GetType());
	}
    }
}

Output

System.Int16
System.Int32
System.Int64
System.Int16
System.Int32
System.Int64

Which should I use?

So should you prefer Int16 to short, Int32 to int, or Int64 to long? Not usually; conventions in most C# programs prefer the short, int, and long types. However, in some programs where the number of bytes in the types is most important, you might prefer Int16, Int32, or Int64. In methods where you use int variables, it is a poor choice to use Int32 variables instead, as int is very standard.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s