>> We just spoke about combining branches and loops.
Now we're going to talk about arrays,
lists and collections in CSharp.
>> I'm going to switch back to where we were before.
We were just working on our branches,
loops, click on arrays and lists.
We're going to go through this basic list example.
Now, they're telling me to make a new application,
but because I know now what Hello World looks like,
I can just delete that one and keep reusing my SIM application.
>> Definitely.
>> I'm going to take just the code inside of the main
here and put that over here,
and let's take a look at what's going on.
>> Yeah. That should give you, there's a little array.
>> Yeah. This is interesting.
We are seeing var which I've never seen before and then new list.
There's actually two things that are going on here.
Why don't we fix that one and you can explain this one here?
>> Sure. Yeah. Arrays always get the priority.
Actually, if you see a little light bulb popping up
in the margin, there it is.
Why don't we go ahead and expand that?
This is recognizing that this type,
rather right now it's
a collection of a generic type, isn't imported.
Remember, we are talking about using statements be in namespaces,
that you could use the type serve of in your program.
This is just warning you, "Hey,
you need a system.collections.genericimports"
statement at the top of your file.
>> If I didn't have using system,
it would complain that console wasn't
there because it's system.console.
>> You get a similar array.
>> List is a system.collections.generic.lists.
I can just go like that.
Look, it put that in there for me,
type that for me and this now doesn't squiggle,
which is pretty sweet. Okay. Cool.
>> Those little light bulbs,
they're often really great hint for you if anything's going wrong,
it should be able to help out.
>> There's lots of light bulbs.
Another light bulb that's popping up here is
we're saying for each which we haven't learned about yet,
but there's a light bulb there, it's
saying you should be explicit.
You should be very specific.
Instead of saying var there,
what is var doing here?
>> Var, I am a pretty big fan of var.
Var is what you put when you do not
know what this variable is going to be.
>> Nor do you care.
>> No do you care. You can just start typing and so it's great.
If I did var,
do you want to create a var like var I equals zero.
>> We've been doing this. We've even making ints this time, right?
>> Right.
>> If I go var I equals zero,
and I go I dot,
I'm getting inty things.
>> Yeah. If you hover over I as well,
I think it will actually indicate that this is an integer.
>> It is.
>> This is just our compiler automatically
inferring that because you have a zero on the other side,
this is just saying, "I'm going to guess,
who's this going to be an integer. "
>> But if I go and say Scott,
I hover over it, it immediately knew that it was a string.
If I say I dot,
look trim, trim in,
those are our stringy things.
Var is cool. If it's like,
"I don't really know, I don't really mind."
I could say, and this is where it gets really useful,
list of string called names, but now,
I've said list string twice,
and it's giving me a headache and it's just faster to type var.
You might find that people
might not like that because it's a style thing, isn't it?
>> It is definitely a code style thing.
This is also something you can configure in
Visual Studio to prefer var when the explicit type,
that being the list of string is apparent,
meaning it's just obvious what this type is.
We don't need further explanation,
and use explicit type in all of their cases.
That's what it sets you now.
If we look at the squiggle under the var in the for each loop,
it's saying that, "Hey, it isn't super obvious what this is.".
>> That is a good point. It is obvious here.
It's not obvious there.
>> Right. Just in the for loop.
There we go. Just in the for-each loop,
we now have a string.
>> Here we're saying with our list of names.
>> Yeah. Definitely a personal lists, much better now.
>> Very important. We're saying,
for each of those names in names and this is very common,
this singular and plural, that's not required.
I could call names foo.
I could say for each name in foo,
but it's nice to say for each name in names,
for each product in products.
>> Names is my list.
When I'm going through my for-each loop,
I am choosing one item in
that list and storing it in that name variable.
We had iterators and indexes in our last examples or counters,
and those would naturally increment every time the loop executed.
With for-each loops, it'll just grab the next item in that list.
It's a good way of transforming anything in a list.
>> That's a good question actually that I'm
having in my mind here. Let me ask you this.
Would it be okay if I did this? Could I do this?
>> Yeah. I think I see where you're going.
>> Right. Then I could take this console.WriteLine.
Could I say name I?
>> You want to use the names?
>> But I see, I got to go to names I, right?
>> Right.
>> That got a lot more complicated.
I get it, but it's not as nice to read as that, is it?
>> Right. You're using the I index
to tell the for-loop
or to tell names which item in the list you're grabbing.
It could be the first or second, third, whatever.
>> We're digging out the zero with the first and the second.
But that is a little harder to read.
>> Definitely.
>> I just want to go through the list.
I can do that,
but isn't it nicer to do it the other way?
But the nice part is that we can do it either way.
>> Yeah, definitely.
>> Yeah. Definitely. Let's switch back over here.
We can go and actually we should see if it works.
Let's do it both ways because who knows.
It did work. This is really important
to point out because it not only worked,
but it in fact worked both ways.
>> Yes.
>> It is showing you that they're the same,
but you want to use the one that makes you happy and
the one that means what you
want it to mean. That's a good reminder.
>> Definitely. Another sort of just style thing.
One of the things that's really fun about programming languages.
It is very much like writing style or just how certain authors
have different writing styles when they
use English or when they're writing different books.
CSharp is very dynamic and personalized.
>> That is a great point. Let's remove all of that.
We'll just start with our list of names.
One of the things that we can do is we can say names.add.
We can add our friends to that list of names.
I'm seeing names dot.
Here's all the things that a name can do.
We can add and can remove.
Then I could say names.remove.
Let's remove me because I've left,
and then we can go and do that for each name in names.
Then we'll say console.WriteLine Let's just go and print out.
We'll just print out the name. Is that cool?
>> Yeah.
>> Then again, I can decide if I want to make
that a string or just a var, totally up to me.
I'll leave it as a var and see if it works.
>> Cool. You've been able to actually
remove and add items to the list of string.
What is list of string?
What is that syntax?
>> Yeah. Isn't that interesting?
That is called generics.
If you're out there searching the internet
and you're finding out what's going on,
you read that as a list of string.
List, they say, can be of any type.
They usually say it can be of any type T. I can have
a list of ints or a list of string or list of pants.
This is actually a programming joke that you taught me.
You can have a cup of T.
>> Because it's a cup of type T.
>> That's your first programming joke in CSharp.
>> A cup of T.
>> If you'd like that,
then you should a programmer.
There's a lot of nerd humor are
available in the programming industry.
Definitely, just casually drink a cup of tea and see
if anyone else notices and I think you'll be a winner.
Then just at the end
here to point out the indexing part that we did,
I can say, names and I can put it into a bracket there.
We talked a little bit about brackets and
arrays when we talked about args, when you explain that.
I could say give me the 0th or the first name.
Let's do this. Console dot write line,
notice how I spell those
wrong and it's going to squiggle on me, isn't it?
It's got to be spelled right and it's
got to be uppercase and lowercase correct.
What is names at one?
It's not names of T. It's lists
of something and it's names at one.
I would expect that would say Scott.
>> Yes. That's another thing to call out.
>> It does not. Why not?
Trick question. I removed myself.
>> Oh, you did remove yourself.
>> Yeah. I removed myself and you are the first one now.
Imagine we had name which is just words.
Scott, Kendra, Maria, Bill, we removed me,
that shifted you up to the number one spot.
>> Yeah. I like it.
>> Now, you're number one in the names list.
>> Speaking of the number one spot,
what's in the 0th spot?
>> That is a weird spot.
I'm actually going to change that.
We'll just say that that's weird.
I will have a spot that's called "Weird".
It's the 0th spot, isn't it?
Everything in an array is zero based.
Zero is the first place.
I don't know why. It's always been that way.
>> It's a big characteristic of
programming languages whether or
not they're zero indexed or start at one.
So CSharp is zero.
>> There are other languages that are one index.
But you just remember that the first spot is the 0th,
the second spot as the first,
and you go from there and you'll be fine.
Those are some of the errors that you'll find as you go and
play with lists is you might be off by one.
That's okay because you'd be able to write it and
try again. All right.
That is a little bit of an understanding of lists and collections.
Let's take a break and we'll come back and
talk about how to search them and sort them.