The document discusses arrays in C# and .NET. It covers declaring and initializing arrays, setting array size at runtime, looping through arrays using for and foreach loops, and using arrays of different data types like integers, floats, and strings. Key points include declaring arrays, assigning values, getting the length of an array, looping from index 0 to length-1, and the differences between for and foreach loops when iterating over an array.
Why use Arrays?Howto set up Array?Arrays and LoopsSet size of Arrays at runtimeForeach loopWhat will we learn?
4.
The variables wehave been working with so far have only been able to hold one value at a timeExample:int lotteryNumber1 = 1;int lotteryNumber2 = 2;int lotteryNumber3 = 3; :An Array allows you to use just one identifying name that refers to lots of valuesWhy use Arrays?
5.
1. Declaration:int[] lotteryNumbers;float[]myFloatValues;string[] myStrings;2. Size of array:lotteryNumbers = new int[4];myFloatValues = new float[10];myStrings = new string[5];How to setup an Array
6.
Declaration and settingthe size in one lineint[] lotteryNumbers = new int[4];float[] myFloatValues = new float[10];string[] myStrings = new string[5];
7.
arrayName[position] = arrayValue;int[]lotteryNumbers = new int[4];lotteryNumbers[0] = 1; // first arraylotteryNumbers[1] = 2;lotteryNumbers[2] = 3;lotteryNumbers[3] = 4; // last (4th) arrayAssigning values
8.
int[] lotteryNumbers =new int[4];lotteryNumbers[0] = 1; lotteryNumbers[1] = 2;lotteryNumbers[2] = 3;lotteryNumbers[3] = 4; First index is ZERO
9.
int[] lotteryNumbers =new int[4];lotteryNumbers[0] = 1; lotteryNumbers[1] = 2;lotteryNumbers[2] = 3;lotteryNumbers[3] = 4; Last index is (SIZE -1)
10.
int[] lotteryNumbers =new int[4];lotteryNumbers[0] = 1; lotteryNumbers[1] = 2;lotteryNumbers[2] = 3;lotteryNumbers[3] = 4; Total number of array = SIZE
11.
int[] lotteryNumbers =new int[4] {1, 2, 3, 4};Declare, Set Array Size and Assign Values in one lineDeclare
To loop throughthe following array:lotteryNumbers[0] lotteryNumbers[1]lotteryNumbers[2] lotteryNumbers[3] for (inti = 0; i != lotteryNumbers.Length; i++){lotteryNumber[i] ….. // not complete}Part 2 Arrays and Loops
15.
for (inti =0; i != lotteryNumbers.Length; i++){lotteryNumber[i] ….. // not complete}starts from 0The first index is ZERO
16.
for (inti =0; i != lotteryNumbers.Length; i++){lotteryNumber[i] ….. // not complete}Length is equal to the SIZE of arrayThe last index should be (Length – 1)
17.
for (inti =0; i != lotteryNumbers.Length; i++){lotteryNumber[i] ….. // not complete}i < lotteryNumers.Length
18.
for (inti =0; i != lotteryNumbers.Length; i++){lotteryNumber[i] ….. // not complete}i <= lotteryNumers.Length -1
19.
New Solution andproject: SpfChapter8Save allChange the project name to "Part 2 Arrays and Loops"Add a button and a listBoxAdd codes into the button click method:Hands On
Looping through from2nd Array to last Arrayint() lotteryNumbers = int(5);for (inti=2; i != lotteryNumbers.Length; ++i){lotteryNumbers(i) = 0;} Spots the Errors
Set size ofArrays at runtimeForeach loopWhat will we learn?
30.
The size ofan array refers to how many items it holdsBut sometimes, you just don’t know how big the array needs to be – for example, when the application depends on user’s input during runtimePart 3 Set the Size of a C# array at RunTime
Compare for loopand foreach loop:for (int i = 0; i != arraySize.Length; i++)foreach (int number in arraySize)foreach loop
35.
Compare for loopand foreach loop:for (int i = 0; i != arraySize.Length; i++)foreach (int number in arraySize)foreach looploop through from 0 to (Length-1) counter
36.
Compare for loopand foreach loop:for (int i = 0; i != arraySize.Length; i++)foreach (int number in arraySize)foreach looploop through from 0 to (Length-1) counterindividual element in array
37.
Compare for loopand foreach loop:for (int i = 0; i != arraySize.Length; i++)foreach (int number in arraySize)foreach loopIndividual element: arraySize[i]Individual element: number
string Array issimilar to integer Array string[] arrayStrings;arrayStrings = new string[5];foreach loop for stringforeach (string arrayElement in arrayStrings)Using foreach with string Array