1 00:00:00,600 --> 00:00:10,380 Hello and welcome to this lecture on arrays and array is a data structure that contains a number of 2 00:00:10,380 --> 00:00:15,660 variables called The Elements of the array. 3 00:00:16,920 --> 00:00:26,730 A variable can hold or store only one value while an array can store multiple values as one. 4 00:00:26,760 --> 00:00:31,910 So an array can store several variables as one value 5 00:00:34,890 --> 00:00:35,910 in C sharp. 6 00:00:36,000 --> 00:00:46,590 The arrays are indexed based that what that means is that all the elements inside an array are usually 7 00:00:47,010 --> 00:00:52,940 numbered by the first element is numbered 0. 8 00:00:52,950 --> 00:00:54,930 The second would be number one. 9 00:00:54,960 --> 00:01:03,240 That's what it mean all the elements in an array must be of the same data type. 10 00:01:03,240 --> 00:01:12,030 For example you can store Strings and store integers in the same array so you either store all strings 11 00:01:12,390 --> 00:01:14,190 or all integers 12 00:01:17,100 --> 00:01:21,570 arrays can also contain other arrays 13 00:01:24,560 --> 00:01:30,620 you can usually identify an array by square brackets. 14 00:01:30,740 --> 00:01:37,050 So most arrays have placed inside a square bracket. 15 00:01:37,160 --> 00:01:44,470 I have got visual studio open so I'm going to show you an example of how we create an array. 16 00:01:44,510 --> 00:01:48,880 When you create an array you can also initialize CRT. 17 00:01:48,890 --> 00:01:51,400 That means you can give it a value. 18 00:01:51,650 --> 00:01:56,720 So to create an array click on new project link this link here. 19 00:01:59,280 --> 00:02:02,970 Or you can go via the File menu. 20 00:02:02,970 --> 00:02:04,110 You can go that way. 21 00:02:04,890 --> 00:02:13,050 So make sure that the templates make sure you called feature will see sharp selected and then select 22 00:02:13,860 --> 00:02:17,340 console application. 23 00:02:17,340 --> 00:02:24,780 Just typing arrays or whatever name you like and click ok. 24 00:02:28,330 --> 00:02:31,530 And wait for Visual Studio to build the project. 25 00:02:32,730 --> 00:02:35,900 So this is a code that Visual Studio has generated. 26 00:02:35,910 --> 00:02:41,090 So in between this curly braces here is where we write any code we have. 27 00:02:41,640 --> 00:02:49,400 So the first thing we need to do we are going to declare an array. 28 00:02:49,590 --> 00:02:57,840 When you declare an array you have to start with the data type that you want to be inside the array. 29 00:02:57,840 --> 00:03:02,380 So I want to declare an array that stores some numbers. 30 00:03:02,520 --> 00:03:14,550 So I start with the data type which is integer followed by square brackets the square brackets indicates 31 00:03:15,510 --> 00:03:19,550 that you're going to put an array in there followed by the name. 32 00:03:19,560 --> 00:03:22,590 I want to call the array. 33 00:03:22,950 --> 00:03:27,570 I'll call it numbers and that will be it. 34 00:03:27,570 --> 00:03:34,530 So what this is done here is saying the variable number is declared but never use. 35 00:03:34,590 --> 00:03:43,870 So I have now declared this array here has been declared as a variable of declared the array. 36 00:03:43,870 --> 00:03:52,080 But notice the Government put any values in the square brackets so it as a stand it's an empty array. 37 00:03:52,680 --> 00:04:03,030 So it can contain any number or the size of the array up not identified it so he can include any number 38 00:04:03,030 --> 00:04:08,860 of variables variable elements inside this array. 39 00:04:08,910 --> 00:04:13,330 All right what I want to do now I want to this array. 40 00:04:13,380 --> 00:04:14,840 I have declared here. 41 00:04:15,660 --> 00:04:19,260 I want to give it a value. 42 00:04:19,260 --> 00:04:25,440 I want it to be a 10 element array and wants to contain 10 elements. 43 00:04:25,530 --> 00:04:33,800 So the way I do that I've already declared it and given it a name so I now call the name which is numbers. 44 00:04:35,480 --> 00:04:37,710 I just select that from the intelligence here. 45 00:04:38,580 --> 00:04:41,070 So that's the name of the array numbers. 46 00:04:41,100 --> 00:04:43,370 So I now want to create new instance of it. 47 00:04:44,140 --> 00:04:52,860 I said that two equals two followed by the word new and then integer 48 00:04:55,500 --> 00:05:00,090 square brackets inside the square square brackets passing 10. 49 00:05:00,960 --> 00:05:02,160 So what this means 50 00:05:05,220 --> 00:05:13,050 what this means is that I want to create a 10 element array. 51 00:05:13,230 --> 00:05:17,250 So this variable now I've not changed from being an empty array. 52 00:05:17,370 --> 00:05:26,580 I have now declared that I want at the values that has to go in it has to be a 10 element array. 53 00:05:26,790 --> 00:05:36,750 Once you have assigned a value to an array you then use curly braces to actually indicate the value. 54 00:05:36,750 --> 00:05:45,770 So I've specified here that this array should be a 10 element array should contain 10 numbers or integers. 55 00:05:46,050 --> 00:05:49,530 So to specify that I need to include curly braces 56 00:05:52,800 --> 00:05:54,620 and just put in some values. 57 00:05:54,630 --> 00:06:02,930 So I'm going to randomly just typing one two three four five six seven eight nine 10. 58 00:06:03,780 --> 00:06:10,880 And when the separate each of each element in the array has to be separated by a comma. 59 00:06:10,960 --> 00:06:17,280 So I put a comma there you don't put a comma on the last value. 60 00:06:17,520 --> 00:06:27,900 So I specify that this is going to be a 10 variable element array and specify the 10 10 element here 61 00:06:29,900 --> 00:06:38,570 the reason they say that C sharp arrays are zero index space is that if I wanted to access for example 62 00:06:39,680 --> 00:06:40,970 this value. 63 00:06:40,970 --> 00:06:46,020 Number three here it all count the way I would access it. 64 00:06:46,040 --> 00:06:51,890 I'll call the name of the of the variable which the name of the array. 65 00:06:51,920 --> 00:06:53,420 So it will be numbers 66 00:06:58,830 --> 00:07:02,920 and then in between the calibrated say I wanted to access this value. 67 00:07:02,970 --> 00:07:11,280 The third value it would be to because the index based creates the first value 0 0 2 should be 1. 68 00:07:11,340 --> 00:07:14,070 That would be 2. 69 00:07:14,130 --> 00:07:20,100 So that's how you access zero based arrays. 70 00:07:20,100 --> 00:07:32,720 So accessing a specific element in the array you use the name of the array you pass in the element index. 71 00:07:32,730 --> 00:07:41,520 So in this case the index is three and you say that equals to 10 ten is the length of the array element. 72 00:07:41,700 --> 00:07:50,250 So if I wanted to access say the haunted to be displayed on the screen this number three here the way 73 00:07:50,250 --> 00:07:52,290 I would do that I would do a console 74 00:07:57,630 --> 00:08:01,260 to console the right line. 75 00:08:01,260 --> 00:08:06,250 This is a built in method inside up pass it. 76 00:08:06,270 --> 00:08:13,920 The name of the variable of the array variable which is numbers and then in square brackets I specify 77 00:08:13,950 --> 00:08:15,240 the array. 78 00:08:15,270 --> 00:08:16,950 Remember it is 0 index. 79 00:08:16,950 --> 00:08:28,650 So this first one will be 0 1 2 and then I put a semicolon and run that so let's kick start to run it. 80 00:08:28,990 --> 00:08:35,790 If you notice when you use the console the right line to execute the code. 81 00:08:35,820 --> 00:08:39,480 Once the code has been executed it does nothing else. 82 00:08:39,720 --> 00:08:47,690 So if you want the details to be displayed on the screen you need to use another command as well. 83 00:08:48,530 --> 00:08:58,010 Another bill 10 method console the read line Screw you Paul semicolon there. 84 00:08:58,860 --> 00:09:00,480 So this is another built in methods. 85 00:09:00,480 --> 00:09:07,330 If you run this if this you add this to this it will actually display the value on the screen. 86 00:09:07,620 --> 00:09:08,780 So let's check that out. 87 00:09:08,790 --> 00:09:13,300 Click Start is you see that it is displayed. 88 00:09:13,300 --> 00:09:16,710 Number three which is the element in the array. 89 00:09:16,720 --> 00:09:24,550 I was trying to target but without the console the red line he just executes the code and exits the 90 00:09:24,550 --> 00:09:24,940 screen. 91 00:09:24,940 --> 00:09:30,890 So if you want the details to be displayed on the screen you need to use the console Dot. 92 00:09:31,030 --> 00:09:37,600 Red line if for example I wanted to display the length of the array. 93 00:09:37,780 --> 00:09:47,330 How that is how the how long the or the total elements I can replace the two and that touch a dot length. 94 00:09:48,680 --> 00:09:51,250 Okay touch it or lend to that. 95 00:09:51,250 --> 00:09:59,440 If I run that it should display I need to remove the square brackets it would display 10 which is the 96 00:09:59,440 --> 00:10:02,950 actual element in the array. 97 00:10:03,610 --> 00:10:08,790 All right so it is display 10 which means there are 10 elements in that array. 98 00:10:09,010 --> 00:10:12,290 So that is it for this lecture on arrays. 99 00:10:12,310 --> 00:10:14,380 Sorry it's taken a bit longer. 100 00:10:14,830 --> 00:10:15,880 Thank you for watching. 101 00:10:15,880 --> 00:10:16,540 Bye for now.