Skip to content

Instantly share code, notes, and snippets.

@jpetto
Created April 4, 2012 15:44
Show Gist options
  • Save jpetto/2302994 to your computer and use it in GitHub Desktop.
Save jpetto/2302994 to your computer and use it in GitHub Desktop.
get the largest number in a List
// declare a list of ints
List<int> ints = new List<int>();
// add some values to our list
ints.Add(13);
ints.Add(78);
ints.Add(3);
ints.Add(45);
ints.Add(22);
// declare a variable to hold the highest value
// initialize it to zero to make sure it has a low starting value
int intHighest = 0;
// loop through each integer in our list
foreach (int i in ints)
{
// if the current value (i) is greater than the current value of intHighest
// change the value of intHighest to the value of i
if (i > intHighest)
{
intHighest = i;
}
}
Console.WriteLine(intHighest);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment