Welcome back.
In this video I want to show you another dictionary that in this case is going to have a key that is
a string and a value that is a string.
So it's really important to understand how versatile dictionaries really are.
And therefore I want to just show you a few different examples.
Right.
So by the way you can also create a dictionary like this where you use the var keyword.
And then you just assign a new dictionary of the types that you want.
So I'm just going to say string.
String.
These are the two types that I want to use.
I'm going to call this one codes.
And the var keyword handles everything else because now it represents a collection of keys and values,
because it understands that I'm assigning a dictionary to it.
So codes will be a dictionary of string string by the way.
Also we can initialize a dictionary straight away using curly brackets and then adding the key value
pairs directly in here using the following approach.
So for example I could have NY which is associated to New York okay.
So you see that the key needs to be within square brackets.
And the value can just be without square brackets.
It can just be a normal string okay.
That's really important to consider.
Now let's create another entry.
Or let me just copy some in here.
So I have California as well as Texas.
So these are just a few codes for states in the US.
Right.
And why Cat.
And I'm assigning some values to it.
So this is just a different way of declaring as well as initializing a dictionary.
Really cool in my opinion.
So now the next thing that I want to show you is how you can get a value, even if you're not sure whether
it exists or not.
So you don't crash your app trying to get a value that doesn't exist.
So I can use this method called dot try get value so it gets the value associated with the specified
key.
Okay, try get value where.
In this case, because our key now is of type string I need to enter a string in here as well.
Now this try get value.
We've seen this before with try parse where we had this out keyword.
Now this out keyword allows us to say where the result will be stored.
So there are two ways how we can do that.
We can either go ahead and create a state variable here for example, and just say out will be state
like.
So without those additional or like with positioning the brackets correctly.
So I'm storing the output in state.
Or I could have just said string state here straight away.
And this will store the output of this.
Try get value which will be a string.
So you see string value.
This will be the actual result, the string itself that is as disposition.
So it will give me New York.
Let's just test this and write it out.
So Console.WriteLine.
And we're going to write the state.
So let's run this real quick to see if this is actually as I say.
And we see it is.
So it says New York.
So what is going on here.
Well what we're doing is we're checking if inside of our state codes we have this value at the key NY.
So try to get value.
You pass the key as well as the out that you want to have.
So where do you want to store the output of the value of the dictionary at the key.
So I just say store it inside of state.
We're creating a new variable which will be only locally available inside of this if statement.
And then we are displaying that particular state okay.
So this try get value is just a safe way to trying to access something.
And if it doesn't exist.
So let's say and one why let's say we use that as a string.
Then it will just do nothing.
So we will not get anything because this here didn't return true.
So this try get value by the way does return true.
So it does return whether it could get the value or it couldn't get the value.
So that's why we can now access New York this way, or CA or TX or whichever option we want to have.
Okay.
So now just quickly with this particular example as well, let's create the for each loop just as we've
done before.
So item in quotes we're going to say for every single item let's print out the state.
Is or the state code is.
And then we use curly brackets here where we say item dot.
And let's add a dollar sign so that the IDE actually helps us.
So item dot key and the state name is.
And then we're going to use curly brackets again.
And we're going to say item dot value okay.
And let's put that in a separate line so we can actually read it properly.
Now let's run this and we see the state code is now NY and the state name is New York okay.
So that's pretty much it for this video.
Now you saw another variant of a dictionary using strings as keys and strings as values.
Now you can use strings as keys and all kinds of other objects as values as well.
Now you could use it like an actual dictionary, where you have one word in one language and another
word in another language.
All right, so that's enough for this video.
I'd say see you in the next one.