C# Arrays

4,887 views
4,591 views

Published on

C# Arrays

Published in: Education
4 Comments
3 Likes
Statistics
Notes
No Downloads
Views
Total views
4,887
On SlideShare
0
From Embeds
0
Number of Embeds
102
Actions
Shares
0
Downloads
222
Comments
4
Likes
3
Embeds 0
No embeds

No notes for slide

C# Arrays

  1. 1. Chapter 8<br />C# .NET Arrays<br />
  2. 2. Pascal case<br />AddUp(..)<br />Camel case<br />firstNumber<br />C Programming Language case<br />first_number<br />a word on naming conventions<br />
  3. 3. Why use Arrays?<br />How to set up Array?<br />Arrays and Loops<br />Set size of Arrays at runtime<br />Foreach loop<br />What will we learn?<br />
  4. 4. The variables we have been working with so far have only been able to hold one value at a time<br />Example:<br />int lotteryNumber1 = 1;<br />int lotteryNumber2 = 2;<br />int lotteryNumber3 = 3;<br /> :<br />An Array allows you to use just one identifying name that refers to lots of values<br />Why use Arrays?<br />
  5. 5. 1. Declaration:<br />int[] lotteryNumbers;<br />float[] myFloatValues;<br />string[] myStrings;<br />2. Size of array:<br />lotteryNumbers = new int[4];<br />myFloatValues = new float[10];<br />myStrings = new string[5];<br />How to setup an Array<br />
  6. 6. Declaration and setting the size in one line<br />int[] lotteryNumbers = new int[4];<br />float[] myFloatValues = new float[10];<br />string[] myStrings = new string[5];<br />
  7. 7. arrayName[position] = arrayValue;<br />int[] lotteryNumbers = new int[4];<br />lotteryNumbers[0] = 1; // first array<br />lotteryNumbers[1] = 2;<br />lotteryNumbers[2] = 3;<br />lotteryNumbers[3] = 4; // last (4th) array<br />Assigning values<br />
  8. 8. int[] lotteryNumbers = new int[4];<br />lotteryNumbers[0] = 1; <br />lotteryNumbers[1] = 2;<br />lotteryNumbers[2] = 3;<br />lotteryNumbers[3] = 4; <br />First index is ZERO<br />
  9. 9. int[] lotteryNumbers = new int[4];<br />lotteryNumbers[0] = 1; <br />lotteryNumbers[1] = 2;<br />lotteryNumbers[2] = 3;<br />lotteryNumbers[3] = 4; <br />Last index is (SIZE -1)<br />
  10. 10. int[] lotteryNumbers = new int[4];<br />lotteryNumbers[0] = 1; <br />lotteryNumbers[1] = 2;<br />lotteryNumbers[2] = 3;<br />lotteryNumbers[3] = 4; <br />Total number of array = SIZE<br />
  11. 11. int[] lotteryNumbers = new int[4] {1, 2, 3, 4};<br />Declare, Set Array Size and Assign Values in one line<br />Declare<br />
  12. 12. int[] lotteryNumbers = new int[4] {1, 2, 3, 4};<br />Set Size of Array<br />
  13. 13. int[] lotteryNumbers = new int[4] {1, 2, 3, 4};<br />Assign values<br />
  14. 14. To loop through the following array:<br />lotteryNumbers[0] <br />lotteryNumbers[1]<br />lotteryNumbers[2] <br />lotteryNumbers[3] <br />for (inti = 0; i != lotteryNumbers.Length; i++)<br />{<br />lotteryNumber[i] ….. // not complete<br />}<br />Part 2 Arrays and Loops<br />
  15. 15. for (inti = 0; i != lotteryNumbers.Length; i++)<br />{<br />lotteryNumber[i] ….. // not complete<br />}<br />starts from 0<br />The first index is ZERO<br />
  16. 16. for (inti = 0; i != lotteryNumbers.Length; i++)<br />{<br />lotteryNumber[i] ….. // not complete<br />}<br />Length is equal to the SIZE of array<br />The last index should be (Length – 1)<br />
  17. 17. for (inti = 0; i != lotteryNumbers.Length; i++)<br />{<br />lotteryNumber[i] ….. // not complete<br />}<br />i < lotteryNumers.Length<br />
  18. 18. for (inti = 0; i != lotteryNumbers.Length; i++)<br />{<br />lotteryNumber[i] ….. // not complete<br />}<br />i <= lotteryNumers.Length -1<br />
  19. 19. New Solution and project: SpfChapter8<br />Save all<br />Change the project name to "Part 2 Arrays and Loops"<br />Add a button and a listBox<br />Add codes into the button click method:<br />Hands On<br />
  20. 20. Use Loop<br />
  21. 21.
  22. 22. Use Loop<br />
  23. 23.
  24. 24.
  25. 25. 4<br />
  26. 26. change the size of Array to 49:<br />
  27. 27. Looping through from 2nd Array to last Array<br />int() lotteryNumbers = int(5);<br />for (inti=2; i != lotteryNumbers.Length; ++i)<br />{<br />lotteryNumbers(i) = 0;<br />} <br />Spots the Errors<br />
  28. 28. Why use Arrays?<br />How to set up Array?<br />Arrays and Loops<br />Review<br />
  29. 29. Set size of Arrays at runtime<br />Foreach loop<br />What will we learn?<br />
  30. 30. The size of an array refers to how many items it holds<br />But sometimes, you just don’t know how big the array needs to be – for example, when the application depends on user’s input during runtime<br />Part 3 Set the Size of a C# array at RunTime<br />
  31. 31. Add another button and textbox<br />Continue from previous project<br />
  32. 32. Add codes for button2 Click method:<br />
  33. 33. The size of the array is set only during runtime<br />
  34. 34. Compare for loop and foreach loop:<br />for (int i = 0; i != arraySize.Length; i++)<br />foreach (int number in arraySize)<br />foreach loop<br />
  35. 35. Compare for loop and foreach loop:<br />for (int i = 0; i != arraySize.Length; i++)<br />foreach (int number in arraySize)<br />foreach loop<br />loop through from 0 to (Length-1) <br />counter<br />
  36. 36. Compare for loop and foreach loop:<br />for (int i = 0; i != arraySize.Length; i++)<br />foreach (int number in arraySize)<br />foreach loop<br />loop through from 0 to (Length-1) <br />counter<br />individual element in array<br />
  37. 37. Compare for loop and foreach loop:<br />for (int i = 0; i != arraySize.Length; i++)<br />foreach (int number in arraySize)<br />foreach loop<br />Individual element: arraySize[i]<br />Individual element: number<br />
  38. 38. Continue from previous project (Extra)<br />
  39. 39. string Array is similar to integer Array<br /> string[] arrayStrings;<br />arrayStrings = new string[5];<br />foreach loop for string<br />foreach (string arrayElement in arrayStrings)<br />Using foreach with string Array<br />
  40. 40. Continue from previous project<br />
  41. 41. string array using for loop<br />
  42. 42.
  43. 43. string array using foreach<br />

×