C# Int Type

Value Int

Int keyword

Int is an integer numeric type. Although there are many different number representations you can use, the int type is the native integer. This provides some advantages. To use the int type in the C# language, you specify the int keyword.

Uint Example Keywords
System.Int32 information

int.MinValue = -2147483648
int.MaxValue =  2147483647

This C# article shows the int type. This 4-byte integer type is common and useful.

Example

Note

In this example, we show how you can declare and assign an integer in a statement. This integer will be placed into a local variable slot in the stack memory. You can use the Console.WriteLine method to output the integer to the screen in a console program. Also, you can compare two integers (variables or values) using the == (equality) operator: this simply compares all the bits for equivalence.

Console.WriteLine

The int type also has MinValue and MaxValue properties—these are useful for when you want to loop through all integers. The second part of the program shows that each integer, when allocated as part of an array, will occupy four bytes.

int.MaxValue Example Array Types
Program that uses an int type [C#]

using System;

class Program
{
    static void Main()
    {
	// Demonstrates an int value type used as local variable.
	int number = 11;
	Console.WriteLine(number);
	number = -1; // Can be negative
	Console.WriteLine(number);
	Console.WriteLine(number == -1); // Can use == operator
	Console.WriteLine(number + 100); // Can use + operator
	Console.WriteLine(number.GetType());
	Console.WriteLine(typeof(int));
	Console.WriteLine(int.MinValue);
	Console.WriteLine(int.MaxValue);

	// Find the memory usage for an int value in a large array.
	long bytes1 = GC.GetTotalMemory(false);
	int[] array = new int[1000 * 1000];
	array[0] = 1;
	long bytes2 = GC.GetTotalMemory(false);
	Console.WriteLine(((bytes2 - bytes1) / (1000 * 1000)).ToString("0 bytes per int"));
    }
}

Output

11
-1
True
99
System.Int32
System.Int32
-2147483648
2147483647
4 bytes per int

Int arguments

Method call

Next, this example demonstrates the usage of an int as a parameter of a method. You can pass a variable or a constant to the method that uses an integer parameter. The arguments are copied to the new method whenever it is called, but this cost is reduced when functions are inlined by the JIT compiler. The program demonstrates the boolean method pattern, which can help simplify complex tests.

JIT Method Test Odd and Even Numbers
Program that uses int argument to method [C#]

using System;

class Program
{
    static void Main()
    {
	// Use integer type as argument to method.
	bool result = IsOdd(1);
	Console.WriteLine(result);

	// Test call the method with different integers.
	result = IsOdd(6);
	Console.WriteLine(result);

	result = IsOdd(100);
	Console.WriteLine(result);

	result = IsOdd(101);
	Console.WriteLine(result);
    }

    static bool IsOdd(int number)
    {
	// Use the integer parameter in the method body.
	return number % 2 != 0;
    }
}

Output

True
False
False
True

Performance tips

Performance optimization

Typically, the native integer type is the fastest in execution speed because the runtime designers have optimized the int to match the current hardware. So for loops and local variables, an int is the best.

Byte Type Short Type Ushort Example

Note: If your program needs to store many thousands or millions of integers in memory, using a more compact type that requires less memory would be faster. More compact types include the byte type, short type or ushort type.

Summary

The C# programming language

The int type in the C# programming language is one of the fundamental types that is found in almost every program, and for good reason: it is exceedingly useful and simple to understand. The key points we saw here are that the int is considered a native integer; that it has MinValue and MaxValue properties; and that it requires four bytes of memory when allocated as part of a larger array.

Number Examples
.NET