C
C#16mo ago
Masmoudi

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
Masmoudi
MasmoudiOP16mo ago
it shows a red line under "Get" even though i wrote what the youtube guy wrote
mg
mg16mo ago
The red line could be one of several things. When you mouse over it, what does the popup say?
Masmoudi
MasmoudiOP16mo ago
No description
mg
mg16mo 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
Masmoudi
MasmoudiOP16mo 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
mg16mo ago
Weird
Jimmacle
Jimmacle16mo ago
Thinker
Thinker16mo ago
Yeah that is... absolutely wrong
Jimmacle
Jimmacle16mo 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
Masmoudi
MasmoudiOP16mo ago
im suing my teacher wtf
Jimmacle
Jimmacle16mo ago
i wouldn't be surprised if this was just some chatgpt generated trash that they didn't check before posting
Masmoudi
MasmoudiOP16mo ago
my teacher legit gave us the link to that
Thinker
Thinker16mo ago
lazy teacher
Jimmacle
Jimmacle16mo ago
the article has other issues like using the Convert class to parse numbers and referring to Int16 instead of short
mg
mg16mo ago
Yeah I was trying to find if there was some historical Dictionary.Get() method but the article is from august 2023
Masmoudi
MasmoudiOP16mo ago
do i use this instead?
Jimmacle
Jimmacle16mo ago
what mg suggested is the simplest solution
Thinker
Thinker16mo ago
wtf, how
Jimmacle
Jimmacle16mo ago
but it will throw an exception if the key doesn't exist
Masmoudi
MasmoudiOP16mo ago
i see
mg
mg16mo ago
No I mean the C# corner article is from aug 2023, so it wouldn't matter if Get() existed in an old version
Masmoudi
MasmoudiOP16mo ago
and TryGetValue will js say it doesnt exist?
mg
mg16mo ago
TryGetValue() exists
Jimmacle
Jimmacle16mo ago
TryGetValue returns true or false if it finds the value or not
Masmoudi
MasmoudiOP16mo ago
i see
Thinker
Thinker16mo ago
yes that's what I meant, how did it get that wrong
mg
mg16mo ago
@Chaqueta ✪ do you have a link to the youtube video? honestly i think jimmacle might be right that it's chatgpt garbage
Masmoudi
MasmoudiOP16mo ago
uh i tried finidng it but i had my history off TvT it was from 2017
Jimmacle
Jimmacle16mo ago
i feel like i've heard people trash on csharpcorner's quality before idk if i'm confusing it with another website
mg
mg16mo ago
I thought it was ok but clearly not
Masmoudi
MasmoudiOP16mo ago
bruh im only dissapointed cus
Masmoudi
MasmoudiOP16mo ago
No description
Masmoudi
MasmoudiOP16mo ago
translates to : use this website to learn about dictionary ish
mg
mg16mo ago
gross
Masmoudi
MasmoudiOP16mo ago
i thought i liked him nvm aight anyways back to coding
mg
mg16mo ago
these microsoft learn pages are published by microsoft who maintains C#, so they will always be the best source of truth
mindhardt
mindhardt16mo ago
.Get sounds like java thing, like with their ArrayList<>s and not allowing anything but array to have an indexer
jcotton42
jcotton4216mo ago
I have It's bad

Did you find this page helpful?