Hey guys welcome back.
In this video we'll be looking at areas and collections in C sharp.
No I said areas and collections because well our Ari is the most widely used type of collection in most
if not all languages.
So my understanding honoree in C sharp you can apply pretty much the same concepts to C++ Java and all
other programming language.
That being said however there are other kinds of collection data types that exist and b since we are
using C sharp we be looking to areas and we'll also be looking at lists and then I'll give you a sneak
peek at the others.
But the reality is that they all kind of behave the same way and I've been on the day it's almost a
matter of preference which one you will choose no to get started.
I'm going to ask you to just pause right here.
Go ahead and create your new project in your solution called arrays and collections and once you've
done that then you can continue in the video.
So let's take some time to define an array and then just put in some visual guides in the form of comments
so that we know exactly what we'll be accomplishing as we go along but an array is a collection of objects
of the same data type.
Or if you wanted a big word to use or do you see it's a homogenous collection of data.
So that really means that you can have a bunch of numbers all directly related to each other or a bunch
of strings all they're related to each other and all the advantage to using an array instead of variables
that we've been using up until this point would be that if I wanted a program to predict the grades
for 10 students in a class all right I would have to declare 10 variables that would be Grade 1 grade
to grade three through ten to collect ten grades instead of doing that however I can declare an every
four 10 hour so we're going to get started with our first array by the clearing an array to store five
grades for some students are at.
So once again if we wanted to store five years origin originally would have been declaring five variables
grade one grade to grade through Grade Four at five.
However we can actually see the data type which in this case would be something like int it for certain
grades.
In this fine and then we would have square brackets that indicates that we want an array.
So we want an array or collection of integers.
Then we give our array a name.
So in this case like I said we're studying grids so inch square brackets grids and then we have to initialize
it.
So we say is equal to a new INT.
And then while this is the sexual we're doing a fixed size area so we know that we want five grades.
So inside of the square brackets are going to see five.
All right.
And then the semicolon so you see that this initialization kind of looks a little bit like call we initialize
our classes right into objects sorry.
So we have the data type and the re the square brackets indicate that it's on our race it looks just
like a variable declaration really except where initializing it like what we did it with the objects
and world class.
So we say give me a collection of integers and I want up to five OK.
So now that we have it declared we can start assigning values and there are a few ways to do that but
we'll do that later on in this section where we assign both fixed theory but then you see that I have
a fixed every section and then I have a variable size resection.
So this is when I know that I have five grades.
So coding.
All right.
No actually I'm went to rearrange it so I'm going to try and do each one in its own section.
Someone just show you how to initialize an array when you don't necessarily know how many you're going
to have and the numbers into variance should come in so that they can do with a fixed size one time
and then the variable size at another point but pressing ahead when I don't necessarily know how many
grades to expect.
Right.
So if you're building this application maybe to sell to various schools some schools may have 10 students
some have fired some of 20 you don't want to put in a fixed size because you can't go above the fixed
size and if you go any less than the fixed size then you're basically wasting space because it is going
to reserve five spaces in memory for grades.
So this time I'm just going to say it's grades and I'm going to call this one grades 1 and remember
I recently in square brackets grades 1 and then semicolon.
So right here I'm saying that I want an r re and I'm not necessarily sure how much space I'm going to
need for this area but just know that I need NRT.
All right.
So I'm just going to just give me a moment that me organize this.
All right.
So I just did a quick crew organization off the comments.
If you didn't read the comments that's fine I'm just doing it as a result.
It's a we can be clear as to what activity we're completing at which point.
So when we're dealing with an R RI off a fixed size what happens once again is that we have to declare
how much space we need know to assign values to this area I have two options I can either see the uranium
which is grades and then choose the subscript.
So what happens is that by seeing five we have reserved five spaces in memory for this three or four
the values that should be going into this area.
So then our address is for the values would be what we call a subscript.
Right.
So each subscript has a speak a name and they start from zero and they go up to one less than the size.
All right so let me just write a comment explaining that.
So when you have five spaces it means that you have addresses zero through four.
So address zero is one then one is then the second space then two is the third space three is the fourth
space and then four is the fifth space.
So in our is zero is very very very valuable.
All right.
So you know in regular math you may not code zero as a speech but in computer zero is the very first
space.
So technically if mathematically if you will have an array of size 10 then you're going to go from zero
to nine or whatever size you declare it is basically zero to end minus one where n is the size.
All right.
That's a little formula.
All right said in another comment If n is the size of the array then your area addresses are 0 2 and
minus 1.
So once again if a 5 is the size then we have zero to five minus 1 which is 4.
All right.
So to assign the values then I would just call each one by its specific address.
So if I wanted to put something in the first one then I say grid's square brackets putting zero as a
subscript or the address and then I would see equals.
That's a one.
All right.
So that's the first grade and then I'm just going to duplicate this to speed it up so I can see grades
subscript 1 subscript 2 subscript 3 subscript 4.
All right.
And then I can just assign the different values here.
So we have some students doing well and some important.
That's fine.
All right.
So another way that you can assign values would be to see grades is equal to and then you have to declare
that you are sending a new every and then the use of curly braces sorry.
And you into area for the data type so I have to say new and then the data type of the type of rape.
So of course if I put a different data type here I'm going to get an error because we already declared
that grades is an integer area type.
Right.
So that's what we mean by it's homogenous in an area kind of stirring strings and integers.
All right.
It has to be one at a time.
So I would say grades and then new and then the ins with the curve with the square braces.
And then in the current braces then I can spell out the values.
So if I wanted so score 0 to have a particular value which in this case I would put 10 and then comma
and then 20 comma 30 comma for it to come a 50.
All right so no I have one two three four five values and then they kind of implicitly assign them to
the appropriate address.
So this is the first value.
It will go into subscript zero.
So I'm setting values here and then I'm setting values here.
What I'm really doing is overwriting these values with this setup right here.
All right so I don't mean to confuse them just showing you that we could have done it this way and we
also could have initialized the three that we write so I can actually just comment.
Well whichever one you want to come in.
No the danger would be in assigning an extra one.
So if I had put in an extra value without putting an extra address like that's it grades five and then
gave it to another value or I or I said come on 60 here I said I have five addresses but I'm assigning
six values.
One two three four five six are counting here one two three four five six.
That's going to be a problem.
And if I attempt to I'm going to execute this and show you the problem that we're going to run into.
All right so all I do was execute and then it's telling me that I have a system that index all its offerings
exception.
So they're telling me that this index was outside its index is another word for subscript R address.
Right.
So this index was outside the bones of the array.
So once again if you set it for five then you're expected to have no more than five.
You can have fewer but no more than art.
So that is how strict areas are when they are assessing the data that is being put in.
I'm just going to remove these values that will would throw an arrow no less of a dimension to assigning
values to an area would be the fact that you wouldn't sit down and you know hard core these voters like
the scenario game initially was that you're building a program to give to a school or some institution
to be able to collect the values from what the program is going to allow the users to input the values
into the Ari.
All right.
So we already know how to inputs we already know how to Oakwood.
We already know how to store values inside of a variable when we allow the user to input data.
So we just want to extend that knowledge of beats to the RV.
So if I told an institution that if they have five students I could give them a program somebody is
going to come in these lines oats those are no examples but I'm giving them a program that they can
at will put in their five students.
All right.
So what I would want to do then is have something like a for loop so I could save for normal loops and
a for loop is basically perfect for an hour.
So let's go through this together.
Someone to save for int is equal to zero.
And remember that all areas start going thing at zero.
Right.
So I starts off with a value of zero and then I'm going to say I want this program or this for little
brother to run for less than five.
So remember that if we say less than five we will run from zero to five minus one.
That sounds very familiar at zero to five minus one.
All right.
And then I will say I want I increment after each run.
So this for loop is going to help us to do is go through each of the the subscript or the index is up
for every quite weekly right.
All right.
So I just wrote in to console that red line still very easily answer at this point.
That shouldn't be very hard for a second pause the video.
Write them off.
But then what I'm doing here is just prompting the use of something.
Enter a student grades that I'm just seeing console right.
So this is a right line where it would break the line and then the right is what would happen for as
many times as the loop is run.
So if I run it right.
No is going to ask you to enter the grade 510 CTC enter student grades and then the right integrated
integrate integrate integrate off course the write those and the line.
So that's why I did that.
However all the data was sprints.
Obviously that's not what we're here for we wanted to actually pause.
Allow me to input the grade maybe press enter and then it proceeds.
Right.
So what I would do here is one say that I want the grade with the index of what ever value I has.
So remember if I starting off a zero and all three start off at zero then instead of seeing grade with
a hard coded index here like we did when we set Grade Zero is this period 1 is that group 2 is that
blah.
We can actually just see agreed with the subscript our index value of i.
So that means whatever value I has at the time that the for loop is running.
That is the index inside of the three that I want to manipulate and the manipulation here would be to
accept input.
So of course you know that to accept inputs to console read the line all right.
And then because greed is an integer.
Right.
Oh sorry it's not green but grids my.
So grids represents an integer we're getting that red line so we we need to pass.
So we say in that hires what ever value we get from the console.
All right.
So once again we're just seeing enter grid and then we pause for inputs.
That's what this line is doing.
So a possible input to the console that read line when the person presses enter.
Then we pass it to be an integer and then having passed it we assign it to the corresponding index in
the area based on the value that I has.
So this now allows us to run from zero to whatever value we put there which if it's and then it's N
minus one.
All right.
So let's try that.
All right.
So we see here that we printed the console the right line integrates and then where it's integrated.
This is the first run and then it's waiting.
So I'm going to put 10 then when I press enter it runs again.
No it's counted up no eyes 1.
So I'm 20 and 30 then 40 and then 50.
That's 1 1 7 2 3 4 5 values.
And then when I press enter we see that it stops running.
All right.
So that is how we dynamically accept values into the area.
And as you can see it's kind of in a straight line zero to the end it's not put in the first one then
the third one then maybe the fourth one then go back to the second one.
No it's from start to finish right.
No that was just inserting the values into the grid.
So by the end of this fall to all of the values that have been stored instead of grids similar to that
but of course not higher quality would be relative to whatever the user would have put you know at the
time.
No.
When the person in time they're finished I'm sure they'd want to see the grades that they entered.
So I'm just going to copy this code.
All right.
Because then the for loop allows us to go back through the Ari again.
So instead of writing Enter Grades I'm going to see the grades you entered are or were.
Well I can leave that and then I'm going to see instead of pausing for inputs I'm just going to create
all this for loop altogether and then what I'm going to see in this for loop is console dots rights
line and then I want to write the value that is inside of each subscript of my grades I read.
All right.
So then each line is just going to print OK whatever value I has at the time.
That's the value is going to print.
So let's try that again.
All right so norm when to enter the grids and I'm going to be a bit more vague this time so can see
I'm not urging it so I'm putting in random grids 50 into a 46 38 52 and 30 and then we print all the
greats of the grades you entered are in the exact order that they were entered right for all five.
Now if you don't write your for loop properly and then you end up trying to put in an extra grid here
in the area 5 or the fixed size 3 then it will give you one of those old bones that are safe you are
seeing on Audubon's arrows.
It means that you are trying to reference an index that does not exist based on your declaration of
your Ari.
All right.
So know that we looked at how we deal with the fixed size.
Ari let us take a look at how we deal with the variable size our severe variables mean that they're
prone to change or they're basically unknown until a certain situation.
So when we see the variable size it means that I'm giving this program to a school that I don't know
the number of students that have that one or the number of grades that f the input.
So I can't be as rigid as seeing five right or ten or one hundred.
I don't want to say a hundred thousand when there were 50 students and then the computer crashes on
the pressure when it really didn't tough to write.
So what I'm doing here is saying that OTM I know I need an RE I don't know what size it's going to be
but then later on you know based on the size are based on the values that they're putting then I can
make that determination so that every declaration are assigned to every assignment value assignment
in this situation would more model this one right where we just copied and pasted so that we can see
it within its context.
And we're talking about grades 1.
Right.
So because we didn't give it a size initially I can see grades one is equal to.
And whatever is in the curly brace whatever number of values I put in it will accept that as its size.
So if I put in five then it knows it's an aerial size five.
If I put in 10 all right so those put in five more values looks cheap but lets us work with it.
Then it knows that its size is no 10.
So that's why would call it a variable size.
I agree.
All right.
Know unfortunately based on the nature of these dynamically sized Ari's the based on the strict nature
of Ari's actually it is kind of difficult to actually dynamically assign values.
So after you've defined it it's see they define it here in this land where what I said is equal to a
new into all of this or I would define it here but then you know putting it in a for loop on populating
it like that.
It's kind of more difficult.
So I actually see Shah use those other collection types and one such type is a list.
All right.
So I'm going to use this opportunity you know to show you the list so I'm really and truly printing
diaries in the variable printing the value sorry and the variable error is very similar.
So we just do that quickly where I'm just going to take this code from the previous already and then
what we can do is actually see something like the Iranian which is grades 1 and then I can see a dot
and then see it at length right.
So by seeing grids 1 dots length I am seeing that I'm not sure how many volumes are here but whatever
number of values there are which is depicted by the volume length.
All right.
So if you just hover over it you say it gets the total number of elements in the dam in all the dimensions
of the tree.
All right.
So you get the length of the entire area and then you run from 0 0 1 to the counter is less than the
length of the area.
And so lengthier arcs the same way as well if we had defined the size.
So because we didn't define a size nor regardless of the number of eyes you put there to find out how
many are there you just see that length.
All right.
But once again the weakness here is that I had to predefined these values and that is not necessarily
the best way to do it.
So we have another bit at time and it's called a list or another collection type.
And it's called the list.
So firstly we're going to declare a list.
So at least declaration is very similar except there are no square brackets so it's a list.
And then let's say that I want a list of strings so this time I want to be storing the names of the
students in the school so I can see a list and then the data type which is string.
All right.
And then give it the name.
So list names.
Right.
So list of type String names is equal to and then we just initialize it the way that we hope list string.
So you'll see in the nuances with the initialization based on the data types and the collection types
that we're dealing with.
But you know as you practice more and more you'll get used to the different four months.
No you'll notice that I have a red line here.
So I have to include another library so by hovering clicking the light bulb I will get system up collections
generic.
So when I click that you will include it and then we can use the list data type.
All right.
No to add to the list.
So this is the clearing the list to add values to the list let me just put in the comment add values
to a list in order to do that.
We simply just see names which is the name of the list dot.
And then when we say dots we see that we have a bunch of functions available to us we can find we can
get the count.
So columns would mean that whatever.
It's just that length.
So Collins does the same thing as a name.
Because as you can see I don't know how many values will be in the list.
So the list is also a variable size we don't know.
It could be one it could be a few it could be none.
Right.
So columns will tell us that.
But to add something we simply do the name of the list which is names not hard.
All right.
And then when we open the braces we see that it's a function that is expecting a parameter of the data
type that the list is representing.
So the list is off type string.
So it is expecting string some string value.
So I can just hardwood a string there if I want so I can see name and I put my name as the first one.
All right.
But then in the and we we can actually do a loop that will allow us to add something from the console.
Now in this situation because it is not counter controlled control or we don't know how many we want
to add or not I wouldn't want to use a for loop because a for loop goes up to a limit at any point.
So I could use a while loop and so what I will do is say something like define a variable called name
and initialize it to a blank string.
So I add some variables up here that I was using for something else.
You can ignore them in case you're concerned about what they were for.
That's fine to remove them so that they don't confuse you.
But right now I'm just going to define a variable called names.
So the purpose of name is to store the value that the user is going to enter into the console.
All right.
I'm going to set the escape character to be like negative one.
All right.
So that means I'm going to use a while loop to see while the value of name is not our name and I want
to use up some string manipulation and also name as a string so if I as a name dot I can get a bunch
of things about it.
Right.
So I can see if name dot equals and then I'm just going to give it the value so I want to see if name
that equals negative one.
Meaning if the value in name equals negative one.
Sorry.
While it is not equal to one.
So then we just do that.
All right so this is a boolean right.
So this is going to say is name equal to the value negative one that I'm seeing while that is not true.
Then I want to continue the execution.
Right.
So let me just put my prompt and just borrow this code set of writing it all out and I'm just going
to say enter names.
All right and then below I'm going to say name is equal to console dots.
Read the line right.
Some are allowing the user to enter a name into the console volume into the console and the damn street
in the name variable.
Right.
And then I'm going to see if an image actually copy this logic so this is called nesting No.
This is where you have one control structure housing another one so you have a loop which is the while
loop brain of a new statement inside the loops they have missed it and your statement inside a while
loop and if statements and while loops act on conditions some would use the same condition right.
So if name is not equal to the value of negative one then I want to add the value that was injured to
the list stuff names.
Right so we've added one name so far so this one is just going to be hardcoded but then dynamically
I want to see that names.
Which is my list not add.
And then the value I'm passing in is the string value that was entered into the name variable.
All right.
So he's going to do that while name is not equal to a negative one.
So that means on the first or end if I enter Emily then it's this is going to evolve into false or true
or other so we add Emily.
Then afterwards if I say negative one then it will skip over this and then it will come back and realize
it does violated the condition and then it's all right.
And then the same way that we could print values in the ivory is the same way that we can print values
in the lists so they're actually quite a few ways one would be using a for loop.
Right so we would see names not honed instead.
So I'm seeing that and the middle standard salt because I'm just borrowing code so I just want to make
sure I'm consistent.
So the students student names you entered are and then for I.
So this seems subscript rules apply to lists right.
So I can actually see names and then call it by its exact index.
So when something goes into the list it has an index just like it would have had in the RV.
All right.
So I can use a for loop to go through each index for the cones are the size of the list or the number
of items in the list and then it would just I to read through that there is also another we call out
for each.
Right.
So this is probably what you see more often than not where you see it for each.
And then what you're seeing this is I want each item in the least start each item in the collection.
So what I would say something like each item is expected to be a string.
So I have to be clear the item as the data type that I'm expecting for from the string.
Right.
If I'm not so sure I can actually use my keyword via here.
So I've been skirting around using var and that's fine but you can't use var or you can use the strong
type.
That's no problem.
So in this case I use a strong type just to drive the point home.
But I'm saying for each string and then I'm calling it.
Item writes for each string item in the list on the list here is called names.
Right.
So for each one of them I want to cancel I mean a copy that one in my right still console on the right
line and then I would print the item.
Right.
So what happens with this loop is that each time it goes through it will take whatever is inside of
names storage an item and then you would be able to print it to the screen.
And then when it runs out of all of the items inside it just exits the loop by itself.
All right.
So this one is using the for loop.
All right.
This one is using the for each loop
all right.
So that's the difference between lists and variables so when you try to.
But then I mean sort of lists and areas.
So if you try to have a dynamic area it might not work code's best based on its nature.
It's very strict like I said before.
But then when you have a need for a dynamic collection you can always call on the list and then you
have all the data types you have.
Numerical.
You have a query ably of I collection I observable you have a bunch of them.
And the reality is that if you want complex data types like a class go we looked at classes and objects.
If you add a class that you needed to store the list of objects of that class then you could use the
list all right.
So I just added some comments just to notice what each block of code is doing know what we're going
to do is run and then we run through each and every one of them.
So the dynamic Ari the FT story and the list.
All right so we're starting off with our fixed three or five grades so we know that this is only going
to run for five data.
Oh so the pricing space I'm not enter.
Right.
So that would also be a problem of price space or putting some other stream that is not you know can't
be deciphered as a number then it will cause a problem.
But we know the this.
Right.
So just putting in five grades and then it shows you that.
Okay.
The five grade the grades you entered are and the grades you entered are and okay I'm seeing an error
here where it is seeing that I have an index all top bones erode.
So let me stop this and see exactly what went wrong.
All right.
So
I see my mistake.
So I was reprinting grades so what happened is that I have the length of grades one for the for loop
but then I didn't change this from grades degrees one.
So that's my mistake.
If you hit that arrow I apologize it's grades 1 for that area.
All right so let's try that again.
All right.
So we proceed to enter our five grades.
All right.
So here we see we print reprinted our five grades.
That's good.
And then we printed Oh it's the ten grades from the dynamic I read right.
And then it meets the province where it's asking us to enter names.
All right someone to enter a name I did it.
Emily went to see Red John and Henry and want more Denise.
All right.
When I press enter and write negative one as my value then it exits the loop.
Right.
So negative one.
It was the exit character and here were reprinting all of the names in the list so remember Tibor we
added that one manually.
Right.
But then all the others we entered as we went along.
And then this is with the for each loop and you can see that the results are pretty much the same.
But for me the syntax for the for each is just much easier to write.
All right so that's basically it with with the lists and collections.
That's a broad idea when you get more advanced.
You see that you can actually use collections and more so lists with bigger data types more complexity
of the types.
I don't want overwhelm me in this lesson but as you go along you will get more advanced.