Concept Flow - Integer types and their ranges
Start
Choose integer type
Check size in bits
Determine range
Use type in code
End
This flow shows how to pick an integer type, check its size, find its range, and use it in code.
int a = 100; byte b = 255; short c = -32768; long d = 9223372036854775807;
| Step | Variable | Type | Value Assigned | Range Min | Range Max | Valid Assignment? |
|---|---|---|---|---|---|---|
| 1 | a | int | 100 | -2,147,483,648 | 2,147,483,647 | Yes |
| 2 | b | byte | 255 | 0 | 255 | Yes |
| 3 | c | short | -32768 | -32,768 | 32,767 | Yes |
| 4 | d | long | 9223372036854775807 | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | Yes |
| 5 | b | byte | 256 | 0 | 255 | No - exceeds max |
| 6 | c | short | -40000 | -32,768 | 32,767 | No - below min |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 |
|---|---|---|---|---|---|
| a | undefined | 100 | 100 | 100 | 100 |
| b | undefined | undefined | 255 | 255 | 255 |
| c | undefined | undefined | undefined | -32768 | -32768 |
| d | undefined | undefined | undefined | undefined | 9223372036854775807 |
Integer types in C# include byte, short, int, and long. Each type has a fixed size and range. byte: 0 to 255 (8 bits) short: -32,768 to 32,767 (16 bits) int: -2,147,483,648 to 2,147,483,647 (32 bits) long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (64 bits) Assigning values outside these ranges causes errors.
sbytesbyte type stores signed 8-bit integers from -128 to 127.byte stores 0 to 255 (unsigned), short stores -32,768 to 32,767, and int stores much larger values.byte (unsigned) with sbyteshort which has a larger rangeint is smallestuint in C#?uint rangeuint is unsigned 32-bit integer with range 0 to 4,294,967,295.uint number = -1; assigns -1 (negative) which is invalid. uint number = 4294967295; assigns max valid value 4,294,967,295. uint number = 4294967296; assigns 4,294,967,296 which is too large. uint number = 3.14; assigns a decimal which is invalid for integer type.short a = 30000; short b = 10000; int c = a + b; Console.WriteLine(c);
a and b are short (16-bit signed), but addition promotes them to int automatically.int range, so c stores 40000 and prints it.byte x = 255; byte y = 1; byte z = (byte)(x + y); Console.WriteLine(z);
x + y is done as int (255 + 1 = 256). Casting to byte truncates to 8 bits.byte max is 255, 256 wraps around to 0 due to overflow without exception.int max (2,147,483,647) and uint max (4,294,967,295).long is a signed 64-bit integer with range ±9 quintillion, enough to store 5 billion safely.Pass the test and this pattern moves unknown