Array

Types so far

// Number
var count = 5; 

// string
var dayOfWeek = "Tuesday";


// boolean
var isHipToBeSquare = true

though really a data structure

Arrays are a data structure that allows us to store things in a list-like manner 

Before array

var lion1Name = "Simba";

var lion2Name = "Scar";

var lion3Name = "Nala";

var lion4Name = "Mufasa";

With Arrays

var lions = new string[] {"Simba", "Nala", "Scar", "Mufasa"};


 // what will this print out?
Console.WriteLine(lions[2]);

Console.WriteLine(lions[6]);

Lists are cooler

var lions = new List<string>() {"Simba", "Nala", "Scar", "Mufasa"};


 // what will this print out?
Console.WriteLine(lions[2]);

Console.WriteLine(lions[6]);

using Lists

var myFavoriteThings = new List<string>();

// add
myFavoriteThings.Add("Raindrops on roses");

myFavoriteThings.Add("whiskers on kittens");

myFavoriteThings.Add("Bright copper kettles");

// update
myFavoriteThings[1] = "warm woolen mittens";

// delete
myFavoriteThings.RemoveAt(2);

myFavoriteThings.Remove("Raindrops on roses");

// ??

myFavoriteThings[8] = "crisp apple strudels";

bringing it together

var disneyVillains = 
    new List<string> { "Jafar", "Scar", "Hades", "The Shadow Man" };


for (var i = 0; i < disneyVillains.Count; i++)
{
  var currentVillain = disneyVillains[i];
  Console.WriteLine(currentVillain);

}

But wait! there is more....

Really bringing it together

 

var disneyVillains = 
    new List<string> { "Jafar", "Scar", "Hades", "The Shadow Man" };

for (var i = 0; i < disneyVillains.Count; i++)
{
  var currentVillain = disneyVillains[i];
  if (currentVillain == "Hades")
  {
    Console.WriteLine($"{currentVillain} is under rated");
  }
  else
  {
    Console.WriteLine($"{currentVillain}");
  }
}

lets build something.

Copy of arrays and loops c#

By Mark Dewey

Copy of arrays and loops c#

  • 24
Loading comments...

More from Mark Dewey