Hello and welcome to this lecture on dictionaries in C sharp dictionaries are a special type of list
and every value in the list has a key which is also a variable
a great example of a dictionary is a phone book.
I have got used to open so I'm going to create a new project just the console project so I can illustrate
how we can create an example of a dictionary.
So I'm going to click on the new project link here and wait for the templates to load.
I got visual C sharp I'm going to select console application so we've got a default name here.
I'm just going to get rid of that and typing dictionaries
and click OK.
So we wait for the template to load.
Or.
Visual Studio has generated a stub.
That is a sample code.
So in between here is where we're going to write the code any code we have or write it in between this
curly braces.
So the first thing we need to do is to define a dictionary.
And when you define a dictionary you need to specify a key and the type of the value.
For example you you have to specify a string if it's going to be a string or an integer.
OK let's begin.
So we'll start with a named dictionary.
Notice intelligence has given us a clue that I want to specify a dictionary.
So we do that and then we have you have to specified angle brackets in between these brackets.
You have to specify two values.
The first will be the data type.
This is a string
so don't forget with a dictionary you have to have a data type and also you have to have a key.
So we need to have that type of the key.
The type is going to be a string and that type of the value.
So there are two things here.
A type of the key and that type of the value.
So the value is going to be an integer and the type of integer is going to be a long data type.
So now I have yeah if I'm still in the process of defining it.
So you have to separate the key type from the value the type of value separate that with a comma.
The next thing you do you give the dictionary a name.
I'm going to call it phone book and I'm going to create a new instance with new dictionary
that will pass in the type
comma and the value the value is going to be an integer.
And at the end you put parentheses and a semicolon.
So on line 14 here we have defined a dictionary.
OK.
So notice in the definition we have specified the type of key and that type of value.
So in this case the key is the string while the value is the integer which is the long long case an
integer.
Now that we've defined the dictionary we need to find a way to add value to the dictionary.
Now they are two basic ways.
You can use to add a single value to a dictionary.
You can use brackets the brackets operator or you can use the add method.
So we're going to try both.
So let's go ahead and do that.
I want to start with the add method first.
So I've already defined the dictionary as phonebook so I type in phonebook which is here is picked it
up and I need to attach to the add method.
OK.
And a name inside the parentheses are passing the valley.
So say Alex
that would be the type which is a string and then I need to specify the value which is an integer so
I'll type in say for 1 5 5 4 3.
His phone number is not accurate by the way is just an example for number and type pain a semicolon
at the end.
So we have now added this value to the dictionary.
So let's try this second method the second method is to use the brackets operator.
So again I type in the name of the dictionary
and then I'll type in the brackets.
Operator inside the brackets operator I can type in the name.
This will be the string value of comma and then I specify with the brackets.
Operator
You specify an equals to use on equals 2 to assign the value.
So I say 5 1 2 3.
Again this is not a real number.
It's a fictitious number.
So we've now assigned single values to our phonebook dictionary vide two methods we using the add method.
And we also using the brackets operator.
So let's test this to to test it for it to display on the console are retain these two lines of code
here console the right line of pass state phone book which is the name of the form of the dictionary
and then are passed in the value Alex of use the console the red line so that when the application runs
the details remain displayed on the screen.
If I don't use the console the red line it will just execute and the details disappears from the screen.
So this console the read line and enables the details to stay on the screen.
Once the code has been executed.
So what the code on line eighteen is saying that it will display the phone number for Alex.
So let's run that click on the start.
As you can see here either displayed the phone number for Alex.
So that's how you define a dictionary in C sharp.
Thanks for watching and bye for now.