C
C#11mo ago
Rayzenn_

dictionary problem

// using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Metoder_uppgift_41
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> NumberTag = new Dictionary<string, int>();
NumberTag.Add("endre", 911);

int value = NumberTag.Get("endre");
}
}
}
// using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Metoder_uppgift_41
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> NumberTag = new Dictionary<string, int>();
NumberTag.Add("endre", 911);

int value = NumberTag.Get("endre");
}
}
}
38 Replies
Rayzenn_
Rayzenn_OP11mo ago
it shows a red line under "Get" even though i wrote what the youtube guy wrote
mg
mg11mo ago
The red line could be one of several things. When you mouse over it, what does the popup say?
Rayzenn_
Rayzenn_OP11mo ago
No description
mg
mg11mo ago
So there's no Get() method on dictionaries The correct way is to use the indexer NumberTag["endre"] I'm not sure what tutorial you're following but it's definitely wrong
Rayzenn_
Rayzenn_OP11mo ago
i mean thw website my teacher gave did the same thing too https://www.c-sharpcorner.com/UploadFile/mahesh/dictionary-in-C-Sharp/ it shows how u can get it using a .Get method
mg
mg11mo ago
Weird
Jimmacle
Jimmacle11mo ago
Thinker
Thinker11mo ago
Yeah that is... absolutely wrong
Jimmacle
Jimmacle11mo ago
you can use the indexer like mg said or TryGetValue if you aren't sure if the value is in the dictionary to avoid throwing an exception when in doubt always check the official docs and not other websites
Rayzenn_
Rayzenn_OP11mo ago
im suing my teacher wtf
Jimmacle
Jimmacle11mo ago
i wouldn't be surprised if this was just some chatgpt generated trash that they didn't check before posting
Rayzenn_
Rayzenn_OP11mo ago
my teacher legit gave us the link to that
Thinker
Thinker11mo ago
lazy teacher
Jimmacle
Jimmacle11mo ago
the article has other issues like using the Convert class to parse numbers and referring to Int16 instead of short
mg
mg11mo ago
Yeah I was trying to find if there was some historical Dictionary.Get() method but the article is from august 2023
Rayzenn_
Rayzenn_OP11mo ago
do i use this instead?
Jimmacle
Jimmacle11mo ago
what mg suggested is the simplest solution
Thinker
Thinker11mo ago
wtf, how
Jimmacle
Jimmacle11mo ago
but it will throw an exception if the key doesn't exist
Rayzenn_
Rayzenn_OP11mo ago
i see
mg
mg11mo ago
No I mean the C# corner article is from aug 2023, so it wouldn't matter if Get() existed in an old version
Rayzenn_
Rayzenn_OP11mo ago
and TryGetValue will js say it doesnt exist?
mg
mg11mo ago
TryGetValue() exists
Jimmacle
Jimmacle11mo ago
TryGetValue returns true or false if it finds the value or not
Rayzenn_
Rayzenn_OP11mo ago
i see
Thinker
Thinker11mo ago
yes that's what I meant, how did it get that wrong
mg
mg11mo ago
@Chaqueta ✪ do you have a link to the youtube video? honestly i think jimmacle might be right that it's chatgpt garbage
Rayzenn_
Rayzenn_OP11mo ago
uh i tried finidng it but i had my history off TvT it was from 2017
Jimmacle
Jimmacle11mo ago
i feel like i've heard people trash on csharpcorner's quality before idk if i'm confusing it with another website
mg
mg11mo ago
I thought it was ok but clearly not
Rayzenn_
Rayzenn_OP11mo ago
bruh im only dissapointed cus
Rayzenn_
Rayzenn_OP11mo ago
No description
Rayzenn_
Rayzenn_OP11mo ago
translates to : use this website to learn about dictionary ish
mg
mg11mo ago
gross
Rayzenn_
Rayzenn_OP11mo ago
i thought i liked him nvm aight anyways back to coding
mg
mg11mo ago
these microsoft learn pages are published by microsoft who maintains C#, so they will always be the best source of truth
mindhardt
mindhardt11mo ago
.Get sounds like java thing, like with their ArrayList<>s and not allowing anything but array to have an indexer
jcotton42
jcotton4211mo ago
I have It's bad
Want results from more Discord servers?
Add your server