C
C#2y ago
krixsick

❔ ✅ Lists vs arrays vs ArrayLists and jagged arrays and dictionaries vs hashtables

Hello! I'm currently still learning more about collections in C# and I came across about non-generic and generic. I think I understand the difference but correct me if I'm wrong is that generic collections are like specialized containers that can hold a collection of objects, but they are designed to work with a specific type of objects. On the other hand, non-generic are like containers that can hold different types of objects. They are not specific about the type of objects they can store. Because of this, ArrayList, Hashtable, and SortedList are considered as non-generic collections and Arrays, dictionaries, Lists, Hashsets and other stuff are considered generic. Now I'm still new to learning all of this so it probably isn't smart of me to find the difference between all of these and learn each one step by step. But currently some questions I have is 1. how dictionaries are considered a generic collection since can't they store strings and integers (my guess because python dictionaries can store ints and strings) 2. The difference between Arrays, Lists, and an ArrayList. I feel like lists basically outclass an array but the only difference I kinda see is when you initialize them. For an array if you know the amount of entries you can state that and create an array that can only hold like 5 entries. (int [] grades = new int[5];). But im learning arraylists right now and they seem very similar to lists. 3. Are jagged arrays useful? So far I dont think my skills are good enough to use jagged arrays efficiently so I was wondering if they're actually beneficial. Since I am not good at C# lol 4. I know the difference between hashtables and dictionaries is that one is generic and non-generic, but im just confused on how they both aren't considered non-generic
20 Replies
Jimmacle
Jimmacle2y ago
1. dictionaries can store any kind of object, and even using any kind of key as long as the key type meets certain requirements 2. basically, arrays are fixed length and lists can grow dynamically. ArrayList and any other non-generic collection is not something i've ever had a reason to use in a real program 3. they can be depending on the problem you're trying to solve 4. "generics" are a specific C# concept that allow types to be specialized to work with specific other types https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/generics. dictionaries are defined as Dictionary<TKey, TValue> and the part in the <> is what makes it generic
krixsick
krixsickOP2y ago
But for a dictionary, i understand that there's a key and value, but can't the key and values be either a float, string, or integer?
Jimmacle
Jimmacle2y ago
they can be anything
krixsick
krixsickOP2y ago
but isn't a generic collection a specific type of a object? Like specifically strings or ints Instead of having different datatypes
Jimmacle
Jimmacle2y ago
strings and ints are different data types too you could have a Dictionary<MyOwnKey, MyValue> if you want
krixsick
krixsickOP2y ago
Ohh is it because we're looking at the <MyOwnKey, MyValue> not actually what it can hold?
Jimmacle
Jimmacle2y ago
generic type parameters can be used for different things, in the case of a dictionary the first one says what type of key it uses and the second one says what values it can hold
krixsick
krixsickOP2y ago
Yes, but the thing im confused about is that doesn't the data type for the key and value have to be the same datatype because a dictionary is considered a generic collection and a generic collection uses a specific type of a object instead of multiple data types?
Jimmacle
Jimmacle2y ago
if you were to treat it as a flat collection then it would become a collection of KeyValuePair<TKey, TValue> which would be your "specific data type" if i'm understanding you correctly the key and value do not have to be the same type, that's why there are 2 generic type parameters
krixsick
krixsickOP2y ago
Ohh I think I see what you mean For a dictionary in C#
Jimmacle
Jimmacle2y ago
i'm not sure if you're using a specific definition of a collection or not but i think that's causing some confusion
krixsick
krixsickOP2y ago
You have to specify whether the datatype of both the key and value
Jimmacle
Jimmacle2y ago
right
krixsick
krixsickOP2y ago
And once you specify those types then OH OK Yea cause then it's just one specific object Now Omg Got it thank you so much for explanation
Jimmacle
Jimmacle2y ago
if you have any experience with C++ templates, C# generics have some similarities
krixsick
krixsickOP2y ago
Ah I see, I just started learning C# a week ago with just some basic python knowledge xD
Jimmacle
Jimmacle2y ago
you're basically defining a type as a pattern that's compatible with multiple different types instead of hard-coding those types into the type so a Dictionary<TKey, TValue> isn't a type you can use, but a Dictionary<string, double[]> is T... is a common way to name generic type arguments so if you see that you know it's talking about the definition and not a specialization of the type
krixsick
krixsickOP2y ago
Ohh I see got it thank you so much for the clarification I appreciate you explaining these concepts for me Thank you again and have a great day sir! $close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server