Hello and welcome to this lecture on arrays and array is a data structure that contains a number of
variables called The Elements of the array.
A variable can hold or store only one value while an array can store multiple values as one.
So an array can store several variables as one value
in C sharp.
The arrays are indexed based that what that means is that all the elements inside an array are usually
numbered by the first element is numbered 0.
The second would be number one.
That's what it mean all the elements in an array must be of the same data type.
For example you can store Strings and store integers in the same array so you either store all strings
or all integers
arrays can also contain other arrays
you can usually identify an array by square brackets.
So most arrays have placed inside a square bracket.
I have got visual studio open so I'm going to show you an example of how we create an array.
When you create an array you can also initialize CRT.
That means you can give it a value.
So to create an array click on new project link this link here.
Or you can go via the File menu.
You can go that way.
So make sure that the templates make sure you called feature will see sharp selected and then select
console application.
Just typing arrays or whatever name you like and click ok.
And wait for Visual Studio to build the project.
So this is a code that Visual Studio has generated.
So in between this curly braces here is where we write any code we have.
So the first thing we need to do we are going to declare an array.
When you declare an array you have to start with the data type that you want to be inside the array.
So I want to declare an array that stores some numbers.
So I start with the data type which is integer followed by square brackets the square brackets indicates
that you're going to put an array in there followed by the name.
I want to call the array.
I'll call it numbers and that will be it.
So what this is done here is saying the variable number is declared but never use.
So I have now declared this array here has been declared as a variable of declared the array.
But notice the Government put any values in the square brackets so it as a stand it's an empty array.
So it can contain any number or the size of the array up not identified it so he can include any number
of variables variable elements inside this array.
All right what I want to do now I want to this array.
I have declared here.
I want to give it a value.
I want it to be a 10 element array and wants to contain 10 elements.
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.
I just select that from the intelligence here.
So that's the name of the array numbers.
So I now want to create new instance of it.
I said that two equals two followed by the word new and then integer
square brackets inside the square square brackets passing 10.
So what this means
what this means is that I want to create a 10 element array.
So this variable now I've not changed from being an empty array.
I have now declared that I want at the values that has to go in it has to be a 10 element array.
Once you have assigned a value to an array you then use curly braces to actually indicate the value.
So I've specified here that this array should be a 10 element array should contain 10 numbers or integers.
So to specify that I need to include curly braces
and just put in some values.
So I'm going to randomly just typing one two three four five six seven eight nine 10.
And when the separate each of each element in the array has to be separated by a comma.
So I put a comma there you don't put a comma on the last value.
So I specify that this is going to be a 10 variable element array and specify the 10 10 element here
the reason they say that C sharp arrays are zero index space is that if I wanted to access for example
this value.
Number three here it all count the way I would access it.
I'll call the name of the of the variable which the name of the array.
So it will be numbers
and then in between the calibrated say I wanted to access this value.
The third value it would be to because the index based creates the first value 0 0 2 should be 1.
That would be 2.
So that's how you access zero based arrays.
So accessing a specific element in the array you use the name of the array you pass in the element index.
So in this case the index is three and you say that equals to 10 ten is the length of the array element.
So if I wanted to access say the haunted to be displayed on the screen this number three here the way
I would do that I would do a console
to console the right line.
This is a built in method inside up pass it.
The name of the variable of the array variable which is numbers and then in square brackets I specify
the array.
Remember it is 0 index.
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.
If you notice when you use the console the right line to execute the code.
Once the code has been executed it does nothing else.
So if you want the details to be displayed on the screen you need to use another command as well.
Another bill 10 method console the read line Screw you Paul semicolon there.
So this is another built in methods.
If you run this if this you add this to this it will actually display the value on the screen.
So let's check that out.
Click Start is you see that it is displayed.
Number three which is the element in the array.
I was trying to target but without the console the red line he just executes the code and exits the
screen.
So if you want the details to be displayed on the screen you need to use the console Dot.
Red line if for example I wanted to display the length of the array.
How that is how the how long the or the total elements I can replace the two and that touch a dot length.
Okay touch it or lend to that.
If I run that it should display I need to remove the square brackets it would display 10 which is the
actual element in the array.
All right so it is display 10 which means there are 10 elements in that array.
So that is it for this lecture on arrays.
Sorry it's taken a bit longer.
Thank you for watching.
Bye for now.