In this video, you will learn what is a dictionary in C#?
Check out my Complete C# Masterclass here: https://bit.ly/38xC6WW
WPF Mastercalss: https://bit.ly/2PZIpMn
In this video, you are going to learn about dictionaries
You will need dictionaries if you want to become an awesome C# developer
We are currently updating our Complete C# Masterclass and decided to share some amazing videos here, on my YouTube channel.
Full updates will be available within the next couple of weeks on Udemy, so check out the link above and get your Complete C# Programming Masterclass with a discount.
Previously we talked about Hashtables, in this tutorial we will discuss the Generic version of a hashtable which is a dictionary:
Defining a Dictionary
Similar to a hashtable a dictionary will store it’s value in a key-value pair.
We can then use the key to retrieve the value next we will see how to use a dictionary and what special methods it got.
From miscrosoft documentation the dictionary is defined like this: Dictionary (TKey, TValue)
which means we have to specify the types of both our keys and values in the declaration similar to how we define a list List (int) for example but the only difference here is that we need to specify two types.
Adding key-value Pairs to a Dictionary
Dictionaries are basically a collection of key-value pair under the hood so we can add new entries in our dictionary by creating a new
Lets Consider the following Class, We will use it to create an employee directory where we will search in this directory using the role name and in return we will get the employee data
To achieve this a dictionary would work just like magic, for our Key we will use the Role which is a string and we will use the Employee type as a value
Fetching Data from a Dictionary
There are many ways to get values from a dictionary here we will explore few of them
1. Using Indexer, If we already know the key and we want to get it’s value we can access the value of the dictionary just like an array using the dictionary’s indexer
2. Using TryGetValue() method, Similar to a hashtable we must be careful when we access a dictionary because if we try accessing a value in the dictionary with a key that does not exists it will throw an error.
3. Using ElementAt(), this method requires an index (int) and it will return the key-value pair at that index we can use a for loop to access all key-value pairs of our Dictionary.
c# add to dictionary
c# initialize dictionary
c# foreach dictionary
c# get value from dictionary
c# dictionary keys
c# dictionary unity
dictionary in c#
c# dictionary containskey
tutorialsEU offers you free video tutorials about programming and development for complete beginners up to experienced programmers.
This includes C#, Unity, Python, Android, Kotlin, Machine Learning, etc.
Stay tuned and subscribe to tutorialsEU: https://goo.gl/rBFh3x