The Dictionary Data Structure in C# in 10 Minutes or Less

  Рет қаралды 30,515

IAmTimCorey

IAmTimCorey

8 ай бұрын

A powerful list type in C# that is not often used is the dictionary. In this video, I want to quickly go over what it is and how to use it effectively so that you can add this data structure to your toolbox.
Full Training Courses: IAmTimCorey.com
Mailing List: signup.iamtimcorey.com/

Пікірлер: 96
@sparticus1701
@sparticus1701 8 ай бұрын
We use Dictionary quite a bit. It's very helpful when needing to map things. You can basically serialize/deserialize one to JSON and use it as a file format among other things.
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
Thanks for sharing!
@acodersjourney
@acodersjourney 8 ай бұрын
Your videos are always helpful. Thanks!
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
You are welcome.
@lee1davis1
@lee1davis1 8 ай бұрын
Years ago I did tons of research on dictionaries and when I finally understood them I used them constantly.
@path_selector
@path_selector 8 ай бұрын
constantly in more ways than one
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
I’m glad you are finding them useful.
@gwog
@gwog 8 ай бұрын
Great succinct lesson, thanks!
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
You are welcome.
@gikasmarkantonatos2168
@gikasmarkantonatos2168 8 ай бұрын
Great as always. Thanks.
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
You are welcome.
@tmeryhewjsf35
@tmeryhewjsf35 3 ай бұрын
Thanks for the information!
@IAmTimCorey
@IAmTimCorey 3 ай бұрын
You are welcome.
@rainbowsprinklez
@rainbowsprinklez 8 ай бұрын
I LOVE the dictionary. I use it SO much!
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
Great!
@rainbowsprinklez
@rainbowsprinklez 8 ай бұрын
@@IAmTimCorey Do you know if dictionaries are better to use over lists? Idk how much space it uses, but if you wanted to check if an object was in a list, the dictionaries O(1) time is way better than O(n)
@ScottAshmead
@ScottAshmead 8 ай бұрын
Would be great to see a follow up on this comparing Dictionary, List, and Datasets along with best use-case scenarios if you have the time..... Great vid
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/
@BrentHollett
@BrentHollett 8 ай бұрын
My Short Version: * ISet - Unordered unique data, usually for 'gather all the keys'. * IDictionary - Items with a defined key and no requirement to 'search' by other keys. * Datasets - Only when the data has an external source. * IQueryable - Multi-polar search of data, where there's either multiple possible keys or other data in the structure is relevant to lookups. * IList - Ordered set of non-unique data, generally where you only iterate over the whole collection. * IEnumerable - Unordered set of non-unique data, generally only using the interface when you receive data from one or more of the other types but don't want to constraint it.
@dasfahrer8187
@dasfahrer8187 8 ай бұрын
Would be interested in seeing a follow-up comparing a list of tuples vs a dictionary.
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
There isn't really a good comparison here. A tuple holds two or more pieces of information. A dictionary is a key/value pair list.
@niennguyen410
@niennguyen410 8 ай бұрын
@@IAmTimCorey Should return tuple or object (instance of class that hold vaule of result) in the async methods?
@purplepixeleater
@purplepixeleater 6 ай бұрын
depending on the complexity of the object you would choose the best solution for your software @@niennguyen410
@StevenWong1995
@StevenWong1995 8 ай бұрын
Thanks Tim for this great video! But sometimes it's just difficult for me to decide in what situation I should use dictionary.
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
Use it when you are looking things up by the unique identifier.
@Sammy2100
@Sammy2100 8 ай бұрын
Thanks for sharing Tim. I use a dictionary mostly for finding and counting duplicates. if (dictionaryObject.ContainsKey(key) dictionaryObject[key]++; else dictionaryObject.Add(key, 1);
@BrentHollett
@BrentHollett 8 ай бұрын
The .AddOrUpdate method of ConcurrentDictionary actually does this really well. "Add this if it doesn't exist (1), otherwise do this if it does and update the value (++)" even if you don't need concurrent access. It reduces the searching by half. I do kinda wish they'd include AddOrUpdate to the IDictionary interface. Otherwise in the above code you _should_ be using .TryGetValue but the implementation still uses two searches because you're using a value type.
@felipe.500
@felipe.500 8 ай бұрын
Excellent
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
Thank you!
@Allysson635
@Allysson635 2 ай бұрын
That's an amazing video, thank you so much, it's being really useful as I'm preparing for some jobs interview. Btw is this auto suggestion an extension or plugin that you use? Is better than the default one that I have. Thanks!
@IAmTimCorey
@IAmTimCorey 2 ай бұрын
I have GitHub Copilot installed. It is the one adding additional suggestions.
@gnitin0384
@gnitin0384 7 ай бұрын
Very useful video. Could we do sorting within values in a Dictionary?
@IAmTimCorey
@IAmTimCorey 7 ай бұрын
The Dictionary lists the elements by their keys. You could sort the values into a new list, but not internal to the Dictionary.
@HolyRamanRajya
@HolyRamanRajya 8 ай бұрын
Since people use Entity Framework a lot now, I think it is more important to explain hashtables as a concept in general as DbSet derives from HashSet, and Dictionaries are special kinds of it. The reason for the performance bump with Dictionaries in scalable systems is primarily due to it being a selfadjusting Array(List) of LinkedLists. In my country, interview questions are directly asked on this concept of hashtables from 2nd year onwards as most startups heavily use Dictionaries or use Entity Framework.
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
Be careful, since Hashtables and other non-generics shouldn't be used in C# anymore: github.com/dotnet/platform-compat/blob/master/docs/DE0006.md
@HolyRamanRajya
@HolyRamanRajya 8 ай бұрын
@@IAmTimCoreyI was alluding to hashtable as a concept, not the class. And yes non-generics shouldn't be used.
@darkmalk94
@darkmalk94 8 ай бұрын
Thank you M.Corey. Is the intelissense Copilot?
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
It is a mix of Intellicode and Copilot. The two work together.
@Richard-jm3um
@Richard-jm3um 7 ай бұрын
Quick question Tim! What would you suggest if I need to build a Dictionary-like structure but with repeated keys. E. G. if I need to keep track of more than one session for the same User ID ?
@purplepixeleater
@purplepixeleater 6 ай бұрын
you can store an array of times added to the dict and use that to make an identifier to the second repeated key like key1a key1b
@Richard-jm3um
@Richard-jm3um 6 ай бұрын
@@purplepixeleater Thanks!
@misc10k
@misc10k 8 ай бұрын
@IAmTimCorey what extension are you using that enables the auto-complete suggestions shown?
@philipyoxall5441
@philipyoxall5441 8 ай бұрын
yes i would also like to know how you did this
@simon-white
@simon-white 8 ай бұрын
That'd probably be intellicode, can enable in the settings, don't believe any extensions are needed.
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
It is either Intellicode or it is GitHub Copilot. The two are so closely related that I'm often not sure where one stops and the other picks up.
@simon-white
@simon-white 8 ай бұрын
6:12 loving intellicode's concept of what makes a great wishlist 😂
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
Yeah, that was interesting. I regularly put vehicles and houses on my wishlist. For some reason, no one ever buys them for me.
@GodShiru
@GodShiru 8 ай бұрын
I actually use this all the time, to handle data that is usually presented in a table (especially in a database). Might not be quite as efficient as a 2D array, but it is much easier to develop methods around this, IMHO. I've actually created a few types around this.
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
Thanks for sharing!
@MrOudoum
@MrOudoum 8 ай бұрын
That is not true, it always depends on the use case. A dictionary uses a hash function to look up the keys this means it implements a hashtable. So if you perform many lookup based on the keys, then a dictionary might be faster compared to a 2D array.
@HolyRamanRajya
@HolyRamanRajya 8 ай бұрын
@@MrOudoum You took the words out of my mouth. Also want to add something that's not talked about enough in C# which is that Dictionaries and Hashsets are internally both hashtables which is the superior collection compared to Array-List and LinkedLists. Hashtables are essentially a selfadjusting array/List of multiple LinkedLists and plugs the weaknesses of both collections in regards to writing(adding/removal complexity) and reading. The Dbset Class used in Entity Framework is an extension of hashsets which is why LINQ works so well and .NET 7 onwards added specialized improvements between the interaction of Iqueryable, hashset around LINQ, internally using SIMD Vector hacks to extract even more performance. It's an interesting thing to explore and keep tabs on.
@mogtabii
@mogtabii 3 ай бұрын
thx
@IAmTimCorey
@IAmTimCorey 3 ай бұрын
You are welcome.
@Any1SL
@Any1SL 8 ай бұрын
Your next 10 video should be on concurrent dictionaries
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
Thanks for the suggestion.
@vinnypassmore5657
@vinnypassmore5657 8 ай бұрын
Would you this as a default situation?
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
I am not sure what you are asking.
@path_selector
@path_selector 8 ай бұрын
sorry 10 minutes 20 seconds you’ll have to update the title as this is clearly clickbait
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
😂 I know you are joking, but here is why I don’t. First, I didn’t say “less than 10 minutes”. So 10:59 would still be acceptable. Second, the bumper at the end is 21 seconds long.
@gracian_
@gracian_ 8 ай бұрын
Unnaceptable
@path_selector
@path_selector 8 ай бұрын
@@IAmTimCorey yup i was thinking this too haha just joking! thanks for your videos, seriously too useful
@LiveErrors
@LiveErrors 7 ай бұрын
i believe that some people use Dictionaries as their prefered method for writing Factories
@b25671
@b25671 Ай бұрын
At our company we use dictionaries all the time.
@IAmTimCorey
@IAmTimCorey Ай бұрын
Great!
@75rxREDSTONE
@75rxREDSTONE 8 ай бұрын
Why is the font scaling in visual studio so high?
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
So that people can see it on video, even when they watch on their phone.
@kylekeenan3485
@kylekeenan3485 8 ай бұрын
​@@IAmTimCoreyThanks for doing so, I watch all your short videos on my phone.
@rafapioli75
@rafapioli75 7 ай бұрын
​@@kylekeenan3485Me too
@abhilashchalissery4637
@abhilashchalissery4637 8 ай бұрын
I think we can use generic also right instead of int and string... Is that OK or should we stick defining data types... Is there any cons in using generic other than performance issue...
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
That is using generics. We have to define their specific type. We can choose which types, but we have to use some specified type.
@abhilashchalissery4637
@abhilashchalissery4637 8 ай бұрын
@@IAmTimCorey Sir, is there a way to pull out data from the senario below. IDictionary obj = new Dictionary(); string[] Sqlparms = { "@firstname", "@lastname" }; string[] SqlValues = { "firstname", "lastname" }; obj.Add(Sqlparms, SqlValues); how can i get the values from obj...
@abhilashchalissery4637
@abhilashchalissery4637 8 ай бұрын
Basically i was trying to do this as below... public IDictionary SQLParams = new Dictionary(); public string ExecuteInsert(string SP, object obj) { try { SQLParams = (IDictionary)obj; string result = ""; using (SqlConnection con = Main_DB) { if (con.State == ConnectionState.Closed) { con.Open(); } using (SqlCommand cmd = new SqlCommand(SP, con)) { foreach (var item in SQLParams) { cmd.Parameters.AddWithValue(item.Key.ToString(), item.Value); } //cmd.Parameters.AddWithValue("@name", "abcd"); //cmd.Parameters.AddWithValue("@mobileno", "1234"); result = cmd.ExecuteScalar().ToString(); } if (con.State == ConnectionState.Open) { con.Close(); } } return result; } catch (System.Exception) { throw; } }
@Jeroen3001
@Jeroen3001 8 ай бұрын
I would store your example in the following way: IDictionary obj = new Dictionary(); obj.Add("@firstname", "firstname"); obj.Add("@lastname","lastname"); So, in general keep the key type simple (=value type like a string)
@Vastlee
@Vastlee 8 ай бұрын
"That is not often used..." WUT?
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
Compared to other data types that are similar. For instance, I might use a Dictionary once in a small application, but I'll use List dozens of times.
@ericritter46
@ericritter46 3 ай бұрын
Billy Bob knows what life is about lmfao.
@IAmTimCorey
@IAmTimCorey 3 ай бұрын
😆
@joe_origlieri
@joe_origlieri 3 ай бұрын
that is 100% how you spell hoagie
@IAmTimCorey
@IAmTimCorey 3 ай бұрын
😂 Thanks. It just looked weird. But once you start looking at a word, it almost always looks weird.
@onlydarthinvader
@onlydarthinvader 8 ай бұрын
This is the most beginner stuff possible, I am sorry, but who really needs this?
@GodShiru
@GodShiru 8 ай бұрын
Oh, sorry, from now on, he should only post stuff YOU'RE interested in... Sigh. You answered your own question : beginners need it, and his channel is about teaching everyone, not just people of your skill level.
@onlydarthinvader
@onlydarthinvader 8 ай бұрын
@@GodShiru you find it literally f...img everywhere. Don't worry, I unsubscribed, these videos are a waste of my time.
@onlydarthinvader
@onlydarthinvader 8 ай бұрын
@timmaes397 this channel used to different, now it is just beginner stuff that has nothing to give to basically anyone who is already working in the field and not occasional student coder
@caparn100
@caparn100 8 ай бұрын
Oh yeah, I forgot no one is learning C# and no one is a beginner.
@Any1SL
@Any1SL 8 ай бұрын
Beginners
Double the Performance of your Dictionary in C#
15:12
Nick Chapsas
Рет қаралды 65 М.
Sigma Girl Past #funny #sigma #viral
00:20
CRAZY GREAPA
Рет қаралды 13 МЛН
1❤️#thankyou #shorts
00:21
あみか部
Рет қаралды 88 МЛН
Пробую самое сладкое вещество во Вселенной
00:41
How IDisposable and Using Statements Work Together in C#
10:01
IAmTimCorey
Рет қаралды 28 М.
📚What is a Dictionary in C#?
27:26
tutorialsEU
Рет қаралды 29 М.
C# Arrays, Lists, and Dictionaries (Quick dotnet tutorial)
19:51
Dev Leader
Рет қаралды 1,8 М.
Which dictionary to choose in C# and which one is dangerous
11:12
Nick Chapsas
Рет қаралды 100 М.
Уроки C# - Dictionary - Словарь
12:18
XpucT
Рет қаралды 22 М.
C# Hashsets - Understand them, use them, LOVE them
10:28
tutorialsEU - C#
Рет қаралды 11 М.
C# Array vs Dictionary vs List a Beginners Guide in .NET
20:48
Learn Hash Tables in 13 minutes #️⃣
13:26
Bro Code
Рет қаралды 320 М.
Sigma Girl Past #funny #sigma #viral
00:20
CRAZY GREAPA
Рет қаралды 13 МЛН