56 Replies
Dictionary requires two type parameters.
Dictionary<TKey, TValue>
.public Dictionary<string, int> getNameAndAge()
{
return nameAndAge;
}
like that?
the key is a string and the value is an int
🙂
but how do i get the whole list
because i can only call it via the object
not the class
if i make it static?
but i got this
That looks like you tried something like this:
Ahmed
REPL Result: Success
Console Output
Compile: 765.760ms | Execution: 122.947ms | React with ❌ to remove this embed.
nope
Console.WriteLine(Human.getNameAndAge);
Human.getNameAndAge
yep
That's practically exactly what you did.
Human.getNameAndAge
is a method, so you need to invoke it like one:
?
i tried
same thing
Ahmed
REPL Result: Success
Console Output
Compile: 770.151ms | Execution: 121.414ms | React with ❌ to remove this embed.
It's not the same thing
It's different
oh ye i see
Look closely:
i saw
You're getting closer
yeah
So, I imagine what you want is to print all names and ages
ye
in dictionary form
{key, value,
... }
You need to iterate over the elements and print each one like:
hmm
i'd need to iterate a certain number of times too
so i got a question
dict = {key, value [0]
key,value [1]
}
does the first pair count as 0
Iteration statements -for, foreach, do, and while - C#
C# iteration statements (for, foreach, do, and while) repeatedly execute a block of code. You use those statements to create loops or iterate through a collection.
C# is a zero-based language, yes.
Your snippet doesn't make much sense though.
look
this is the snippet im referring to
@The Dark Realm's No. 2 - Hazel
Try using
foreach
instead.
for
will work, but foreach
is really all that's needed here.it's working with the for
how would you do it?
I'll tell you once you have a working version in the thread 🙂
Giving you what I would do won't help you learn 😄
The dictionary has
string
as the key, you can't use numbers from 0 up to get items from it this wayit's for the index of the pair
key = 0, value = 1
and it's iterating over the pairs
It's not though
When you access the dictionary like that it expects the key, not an index.
not yet
I highly recommend trying to use
foreach
instead.Angius
REPL Result: Success
Result: int
Compile: 515.941ms | Execution: 40.232ms | React with ❌ to remove this embed.
This is how you retrieve values from a dictionary
But, yes, if you wanted both keys and values, you would use a foreach
You can technically iterate over the keys using an index IIRC
ima leave this code here n switch to foreach
Best way to learn 🙂
how do i use
foreach
what arguments does it take
Angius
REPL Result: Success
Console Output
Compile: 612.394ms | Execution: 91.784ms | React with ❌ to remove this embed.
?
$tias
it's only showing the key
Trial and error, the basis of all science, including computer science lol
not the value
int length_of_objectCount = Human.objectCount;
Dictionary<string, int> list_for_nameAndAge = Human.nameAndAge;
foreach (var pair in list_for_nameAndAge)
{
Console.WriteLine(pair.Key + pair.Value);
}
i fixed it
how do you get it to appear like
{key, value
... }
in that format
$"{{{pair.Key}, {pair.Value}}}"
should do the trickyou gotta do it by hand?
💀
Well, you could serialize the dictionary to JSON and print it all if you wanted to
Otherwise, yes
nvm
ye thank you
okay i got the hang of it
thank you :)))
I supplied a link to documentation that goes over it.
@The Dark Realm's No. 2 - Hazel u 2 thank you 🙂
i understand now
thank you again
!close
Closed!
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.