TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Int Type

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

Int is an integer numeric type.

There are many different number representations you can use. But the int type is the most common one. It provides some advantages. Int, a keyword, lets us access a signed integer type.

Uint

System.Int32 information

int.MinValue = -2147483648
int.MaxValue =  2147483647

Example. We 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 Console.WriteLine to output the integer to the screen in a console program.

Console.WriteLine

Also: You can compare two integers (variables or values) using the == (equality) operator. This compares all the bits for equivalence.

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.MaxValueArrays

C# program that uses an int type

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. 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. An int argument is a common type.

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 shows the boolean method pattern. This can help simplify complex tests.

JITOdd, Even

C# program that uses int argument to method

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

Parse, TryParse. Often a string contains characters that could be represented in an int variable. We can convert the string into an int with the int.Parse or int.TryParse methods. The int.TryParse method is better for when data might be invalid.

Parse

Performance. In most program contexts, an int performs well. The one consideration where int may impede performance is spatial locality. As a field in a class, it requires 4 bytes. But a smaller type (byte) may be sufficient for small numbers.

Note: If your program needs to store many thousands of integers in memory, a more compact type that requires less memory would be faster.

And: More compact types include the byte type, short type or ushort type. These are separately described.

ByteShortUshort

Summary. The int type in the C# language is one of the fundamental types. It is found in almost every program, and for good reason. It is exceedingly useful and simple to understand. It can be considered the default number type.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf