Hash Tables in C# - How to use and when to use?

Описание к видео Hash Tables in C# - How to use and when to use?

🚀 Master C# and .NET programming EASILY with our best-selling C# Masterclass: https://bit.ly/47Hk3u7

Hastables in C# - how and when to use?

In this video you will learn all about hastables in C#
When to use hastables and how to use hashtables

In this lesson we will learn about Hashtables and dictionaries

Which are one of the most useful collections in C#. We will see how to define them and how to use them and in which case scenarios we should take advantage of them.

To understand the concept of hashing and tables first think about a real dictionary

Assume we have a German/English Dictionary where every word in German corresponds to only one word in English. So when we try and translate the word 'Auto' in German, which translates to 'Car' in English, we used one word as a keyword to search, and we got the translation as a result

Defining a Hashtable

While we are at it, let's put our last example into action, and let's implement a simple application that will create few objects of type 'Student' and store them into a hashtable using their IDs as keys and the object itself as a value.

Adding Entries to our Hashtable

Now we can define and initialize few objects of our Student class

After we initialize them we will add them to our Hashtable we will use our student Id property as a key and the object it self as the value like we discussed earlier

Fetching Entries from our Hashtable

Now let's try fetching the data from our hashtable

We have a few options

The simplest one if we want to access an element that we already have its key is by accessing it using the hashtable object followed by [] just like an array

Since Hashtables are a non-generic collections they will return an object which before we start working with we have to cast it back to type Student
This is exactly how hashtables and dictionaries in C# behave we use a key to get a value.

#hashtable #hashtables
#generichashtablecsharp
c# hashtable get value by key

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

Комментарии

Информация по комментариям в разработке