C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Part A: We declare and assign an int. It will be placed into a local variable slot in the stack memory.
Part B: You can compare 2 integers (variables or values) using the == (equality) operator. This compares all the bits for equivalence.
Part C: The int type also has MinValue and MaxValue properties. These are useful for when you want to loop through all integers.
int.MaxValuePart D: This part of the program shows that each integer, when allocated as part of an array, will occupy 4 bytes.
ArrayC# program that uses an int type
using System;
class Program
{
static void Main()
{
// Part A: demonstrate an int value type.
int number = 11;
Console.WriteLine(number);
number = -1; // Can be negative
Console.WriteLine(number);
// Part B: use operators.
Console.WriteLine(number == -1);
Console.WriteLine(number + 100);
Console.WriteLine(number.GetType());
Console.WriteLine(typeof(int));
// Part C: get minimum and maximum.
Console.WriteLine(int.MinValue);
Console.WriteLine(int.MaxValue);
// Part B: find the memory usage for an int in an 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
System.Int32 information:
int.MinValue = -2147483648
int.MaxValue = 2147483647
System.UInt32 information:
uint.MinValue = 0
uint.MaxValue = 4294967295
Copied: The arguments are copied to the new method whenever it is called, but this cost is reduced when functions are inlined.
AggressiveInliningIsOdd: The program shows the boolean method pattern. This can help simplify complex tests.
Odd, EvenC# 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
Alias: The uint type is the same as the System.UInt32 type in the System namespace. This alias provides for clearer code in programs.
NamespaceTip: When code that uses uint is executed, it is allocated in the method's stack. No dynamic heap allocation is done. This is efficient.
Typeof: The program displays the System.Type object for the 2 numeric types. This shows the struct that uint aliases to.
Sign bit: Uints lack the sign bit that, when set, makes a number equal a negative value.
constC# program that uses uint type
using System;
class Program
{
static void Main()
{
// Declare example unsigned and signed integers.
uint value1 = 100;
int value2 = 100;
// Display values.
Console.WriteLine(value1);
Console.WriteLine(value2);
// Assign the maximum values.
uint max1 = uint.MaxValue;
int max2 = int.MaxValue;
// Display maximum values.
Console.WriteLine(max1);
Console.WriteLine(max2);
// Assign the minimum values.
uint min1 = uint.MinValue;
int min2 = int.MinValue;
// Write the minimum values.
Console.WriteLine(min1);
Console.WriteLine(min2);
// Write the types.
Console.WriteLine(typeof(uint));
Console.WriteLine(typeof(int));
}
}
Output
100
100
4294967295
2147483647
0
-2147483648
System.UInt32
System.Int32
So: For example, try compiling the code "int value1 = 3000000000". The C# compiler will give the error here.
Tip: If you change the "int" type to "uint", your program will compile successfully.
C# program that causes int error
class Program
{
static void Main()
{
int value1 = 3000000000;
}
}
Output
Error CS0266
Cannot implicitly convert type 'uint' to 'int'.
An explicit conversion exists (are you missing a cast?)
Info: A nullable int can be tested directly against null. This makes it different from a class instance that has an int field.
NullableClassC# program that uses nullable int
using System;
class Program
{
static void Main()
{
// A nullable int can be a number, or null.
int? test = 100;
Console.WriteLine("Value is {0}", test.Value);
test = null;
Console.WriteLine("Null: {0}", test == null);
}
}
Output
Value is 100
Null: True
So: 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 can be packed together.