TheDeveloperBlog.com

Home | Contact Us

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

C# int.MaxValue Example

This C# article shows the int.MaxValue and int.MinValue constants. It displays other constants.

Int.MaxValue. An int has a maximum value it can represent.

This value is found with int.MaxValue. The minimum value too can be determined with int.MinValue. Numeric types (int, uint, short and ushort) have specific max values.

Int

Values. First, here we look at MSDN to see what Microsoft says. It states that MaxValue is a "public const int". With a bit of exploration, I found this information about the MaxValue and MinValue constants.

The fields are constant values and can be accessed anywhere with the composite name. By using these constants, you can avoid typing out 10-digit numbers in your programs. They are not equivalent to infinity.

Constants

short.MaxValue:

32767

short.MinValue:

-32768

ushort.MaxValue:

65535

ushort.MinValue:

0

int.MaxValue:

2,147,483,647

int.MinValue:

-2,147,483,648

uint.MaxValue:

4,294,967,295

uint.MinValue:

0

long.MaxValue:

9,223,372,036,854,775,807

long.MinValue:

-9,223,372,036,854,775,808

Example. Here we clarify the logic in loops. One problem I have dealt with is keeping track of the lowest number found. You can use int.MaxValue to start the value really high, and then any lower number will be valid.

Foreach

Beware edges. When you start your variable at MaxValue, you will want to know the constraints in your program. If the MaxValue can occur, then you will need to be careful with the logic. But it would work correctly here.

C# program that uses int.MaxValue

using System;

class Program
{
    static void Main()
    {
	int[] integerArray = new int[]
	{
	    10000,
	    600,
	    1,
	    5,
	    7,
	    3,
	    1492
	};

	// This will track the lowest number found
	int lowestFound = int.MaxValue;

	foreach (int i in integerArray)
	{
	    // By using int.MaxValue as the initial value,
	    // this check will usually succeed.
	    if (lowestFound > i)
	    {
		lowestFound = i;
		Console.WriteLine(lowestFound);
	    }
	}
    }
}

Output
    (All values are lower than int.MaxValue)

10000
600
1

Discussion. In C# and Windows, there is no performance boost in using smaller data types in loops. Using ushort instead of int just creates unclear code. But prefer ushort, short and byte, for data structures—this reduces memory use.

UshortShortByte

Constants. When you have other constants to define, follow Microsoft's example and declare them as "public const int ValueName". This has great performance, is standard, and clear for other developers. It works well with IntelliSense.

Visual Studio

Also: Instead of using int.MaxValue and int.MinValue, consider nullable types, used with Nullable<int> or 'int?'.

Nullable Int

Summary. We saw values and examples for the max and min constants on number types in the C# language. I suggest that you avoid typing out numbers like -2,147,483,648 unless you have to—and here you don't have to.

Tip: Sometimes it is effective to start an int at int.MaxValue when you are looking for the lowest value in a loop.


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