1
00:00:00,630 --> 00:00:11,990
Hello and welcome to this lecture on dictionaries in C sharp dictionaries are a special type of list

2
00:00:13,770 --> 00:00:23,310
and every value in the list has a key which is also a variable

3
00:00:28,890 --> 00:00:33,940
a great example of a dictionary is a phone book.

4
00:00:36,150 --> 00:00:45,180
I have got used to open so I'm going to create a new project just the console project so I can illustrate

5
00:00:45,690 --> 00:00:50,340
how we can create an example of a dictionary.

6
00:00:50,400 --> 00:00:57,690
So I'm going to click on the new project link here and wait for the templates to load.

7
00:00:57,740 --> 00:01:04,610
I got visual C sharp I'm going to select console application so we've got a default name here.

8
00:01:04,620 --> 00:01:10,230
I'm just going to get rid of that and typing dictionaries

9
00:01:15,750 --> 00:01:17,730
and click OK.

10
00:01:18,210 --> 00:01:24,210
So we wait for the template to load.

11
00:01:26,290 --> 00:01:26,620
Or.

12
00:01:26,640 --> 00:01:30,140
Visual Studio has generated a stub.

13
00:01:30,270 --> 00:01:31,410
That is a sample code.

14
00:01:31,860 --> 00:01:39,000
So in between here is where we're going to write the code any code we have or write it in between this

15
00:01:39,390 --> 00:01:41,330
curly braces.

16
00:01:41,400 --> 00:01:46,580
So the first thing we need to do is to define a dictionary.

17
00:01:46,860 --> 00:01:56,340
And when you define a dictionary you need to specify a key and the type of the value.

18
00:01:56,490 --> 00:02:04,730
For example you you have to specify a string if it's going to be a string or an integer.

19
00:02:06,360 --> 00:02:07,930
OK let's begin.

20
00:02:07,950 --> 00:02:13,630
So we'll start with a named dictionary.

21
00:02:13,830 --> 00:02:18,990
Notice intelligence has given us a clue that I want to specify a dictionary.

22
00:02:18,990 --> 00:02:28,070
So we do that and then we have you have to specified angle brackets in between these brackets.

23
00:02:28,080 --> 00:02:31,470
You have to specify two values.

24
00:02:31,500 --> 00:02:36,100
The first will be the data type.

25
00:02:36,120 --> 00:02:37,470
This is a string

26
00:02:40,590 --> 00:02:47,040
so don't forget with a dictionary you have to have a data type and also you have to have a key.

27
00:02:48,660 --> 00:02:52,540
So we need to have that type of the key.

28
00:02:52,580 --> 00:02:57,150
The type is going to be a string and that type of the value.

29
00:02:57,590 --> 00:02:59,090
So there are two things here.

30
00:02:59,320 --> 00:03:04,750
A type of the key and that type of the value.

31
00:03:05,430 --> 00:03:14,790
So the value is going to be an integer and the type of integer is going to be a long data type.

32
00:03:14,790 --> 00:03:21,280
So now I have yeah if I'm still in the process of defining it.

33
00:03:21,330 --> 00:03:34,340
So you have to separate the key type from the value the type of value separate that with a comma.

34
00:03:34,410 --> 00:03:36,970
The next thing you do you give the dictionary a name.

35
00:03:36,990 --> 00:03:50,700
I'm going to call it phone book and I'm going to create a new instance with new dictionary

36
00:03:55,120 --> 00:03:58,070
that will pass in the type

37
00:04:05,460 --> 00:04:11,230
comma and the value the value is going to be an integer.

38
00:04:11,990 --> 00:04:19,760
And at the end you put parentheses and a semicolon.

39
00:04:20,120 --> 00:04:24,750
So on line 14 here we have defined a dictionary.

40
00:04:25,210 --> 00:04:26,070
OK.

41
00:04:26,270 --> 00:04:36,650
So notice in the definition we have specified the type of key and that type of value.

42
00:04:37,310 --> 00:04:45,770
So in this case the key is the string while the value is the integer which is the long long case an

43
00:04:45,860 --> 00:04:48,320
integer.

44
00:04:48,710 --> 00:04:56,750
Now that we've defined the dictionary we need to find a way to add value to the dictionary.

45
00:04:56,780 --> 00:04:59,890
Now they are two basic ways.

46
00:04:59,960 --> 00:05:04,960
You can use to add a single value to a dictionary.

47
00:05:05,120 --> 00:05:11,910
You can use brackets the brackets operator or you can use the add method.

48
00:05:11,930 --> 00:05:14,440
So we're going to try both.

49
00:05:14,450 --> 00:05:18,160
So let's go ahead and do that.

50
00:05:18,320 --> 00:05:20,300
I want to start with the add method first.

51
00:05:20,310 --> 00:05:29,480
So I've already defined the dictionary as phonebook so I type in phonebook which is here is picked it

52
00:05:29,480 --> 00:05:38,560
up and I need to attach to the add method.

53
00:05:38,630 --> 00:05:39,150
OK.

54
00:05:39,150 --> 00:05:43,900
And a name inside the parentheses are passing the valley.

55
00:05:44,340 --> 00:05:47,070
So say Alex

56
00:05:50,190 --> 00:05:59,580
that would be the type which is a string and then I need to specify the value which is an integer so

57
00:05:59,580 --> 00:06:05,260
I'll type in say for 1 5 5 4 3.

58
00:06:05,640 --> 00:06:15,240
His phone number is not accurate by the way is just an example for number and type pain a semicolon

59
00:06:15,540 --> 00:06:15,990
at the end.

60
00:06:15,990 --> 00:06:21,900
So we have now added this value to the dictionary.

61
00:06:21,900 --> 00:06:27,360
So let's try this second method the second method is to use the brackets operator.

62
00:06:27,450 --> 00:06:31,200
So again I type in the name of the dictionary

63
00:06:34,110 --> 00:06:35,760
and then I'll type in the brackets.

64
00:06:35,760 --> 00:06:43,020
Operator inside the brackets operator I can type in the name.

65
00:06:43,020 --> 00:06:51,630
This will be the string value of comma and then I specify with the brackets.

66
00:06:51,650 --> 00:06:52,350
Operator

67
00:06:55,820 --> 00:07:04,100
You specify an equals to use on equals 2 to assign the value.

68
00:07:04,100 --> 00:07:07,010
So I say 5 1 2 3.

69
00:07:07,010 --> 00:07:09,820
Again this is not a real number.

70
00:07:09,980 --> 00:07:12,230
It's a fictitious number.

71
00:07:12,260 --> 00:07:19,470
So we've now assigned single values to our phonebook dictionary vide two methods we using the add method.

72
00:07:19,490 --> 00:07:23,370
And we also using the brackets operator.

73
00:07:24,560 --> 00:07:31,850
So let's test this to to test it for it to display on the console are retain these two lines of code

74
00:07:31,850 --> 00:07:39,200
here console the right line of pass state phone book which is the name of the form of the dictionary

75
00:07:39,680 --> 00:07:46,820
and then are passed in the value Alex of use the console the red line so that when the application runs

76
00:07:47,330 --> 00:07:50,970
the details remain displayed on the screen.

77
00:07:51,080 --> 00:07:57,890
If I don't use the console the red line it will just execute and the details disappears from the screen.

78
00:07:57,890 --> 00:08:03,860
So this console the read line and enables the details to stay on the screen.

79
00:08:04,070 --> 00:08:08,120
Once the code has been executed.

80
00:08:08,480 --> 00:08:15,250
So what the code on line eighteen is saying that it will display the phone number for Alex.

81
00:08:15,320 --> 00:08:18,130
So let's run that click on the start.

82
00:08:18,530 --> 00:08:24,530
As you can see here either displayed the phone number for Alex.

83
00:08:24,590 --> 00:08:31,040
So that's how you define a dictionary in C sharp.

84
00:08:31,040 --> 00:08:33,590
Thanks for watching and bye for now.
