Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
EvilCat70

What is outputted if the value key for TryGetValue doesn't exist? (C#)

Recommended Posts

Posted · Original PosterOP

Hello. I'm making a C# application and was wondering, if I used dictionary.TryGetValue() on a key that doesn't exist, what would the out be?

 

For example, the dictionary has the key type string and a value of int and contains 2 keys, "a" and "b". If I tried to do dictionary.TryGetValue("c", out int value); What would be outputted)

 

Would it be null? Or would it be 0, as Integers can't be null, they are just 0 otherwise.

 

Thanks.


CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to post
Share on other sites

It would return false:

Quote

Returns
Boolean
true if the Dictionary<TKey,TValue> contains an element with the specified key; otherwise, false.

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.trygetvalue?view=netframework-4.7.2

 

The way you're suppose to use this function, as far as I can see, is by checking if what you want to do is a valid thing (e.g. you're not trying to place a string of characters into an int) and do something with the true/false you receive.

 

e.g. in your example, if you tried to input 'c'  into an int, it would return false and you don't try to place that input into the int, while if it returns true, you do place that into the int.


"There's no such thing as perfect. You're beautiful as you are Courage. With all your imperfections you can do anything." - Bathtub Barracuda

"How can someone win if winning means that someone loses" - Scatman John

"We’re here to make you think about death and get sad and stuff.” - Scott Pilgrim

Gear:                           .                                                                                                                         .

Spoiler

PC:

CPU: AMD Ryzen 7 1700 || Motherboard: MSI X370 Gaming Pro Carbon || RAM: 2x8GB G.Skill TridentZ RGB 3200Mhz (running at 2933Mhz) || GPU: Gigabyte Auros RX 580 4GB || Storage: Samsung 960 EVO 250GB + Seagate Barracuda 2TB || Cooling: Be Quiet! Dark Rock 3 || Case: Thermaltake Core G21 TG || PSU: Corsair RMx 750W

Peripherals:

Headphones: Beyerdynamic DT-990 250 Ohm || Mouse: Roccat Kiro || Keyboard: Coolermaster Masterkeys Pro S RGB || Mousepad: CM Storm Swift-RX XXL || Graphics tablet: Wacom Intuos Pen Small

Old PC:

CPU: AMD Athlon X2 255 || Motherboard: ASUS M4A89GTD PRO || RAM: 4x2GB DDR3 || GPU: Nvidia Quadro 512MB || Case: Antec Sonata III || PSU: Antec Earthwatts 500W Bronze

Minibois' Digital Art Thread

Reviews: Coolermaster Masterkeys Pro S RGB || Wacom Intuos Pen Small || Roccat Kiro

Threads: Pokémon Thread || Dutch Talk || Super Smash Bros. Melee Thread

Setup: Current setup

Link to post
Share on other sites
18 hours ago, EvilCat70 said:

Hello. I'm making a C# application and was wondering, if I used dictionary.TryGetValue() on a key that doesn't exist, what would the out be?

 

For example, the dictionary has the key type string and a value of int and contains 2 keys, "a" and "b". If I tried to do dictionary.TryGetValue("c", out int value); What would be outputted)

 

Would it be null? Or would it be 0, as Integers can't be null, they are just 0 otherwise.

 

Thanks.

And out parameters require an uninitialized variable for example the Int will be 0 as it is it's value uninitialized. The way you are supposed to use the tryget is using an If statement condition. Here's the correct way to use it

 

// uninitialized int for the dictionary search
int value;

// try get return true if it finds a key and fill the out parameter with what it found
if(dictionary.TryGetValue("c", out int value))
{
    // we found the key, show the user what you found
    // since it returned true the variable value has been it's value set to what the dictionary contained
    // Take important note that if possible this value is by reference. Here since it's a primitive type (int)
    // the return is by value but if it would have been a class you would have had a reference to it.
    MessageBox.Show("the Key [c] contain the value " + value.ToString());
}
else
{
    // key was not found what do you want to do ? perhaps add that key with some basic value or soemthing else
}

 

Your int could have been null if you had declare your dictionary to use nullable ints which are declared like so :

 

// very normal integer
int myStandardInt;

// integer that allow null value. This is also true for "nearly" all types there is including custom objects
int? myNullableInt;

 

Link to post
Share on other sites

false would be returned, it returns a boolean - same for most types try parsing in C# AFAIK

 

string val = "";
bool success = dictionary.TryGetValue('cookies', out val);
if(sucess) 
	Console.WriteLine("Eat" + val);
else 
	Console.WriteLine("im hungry");

(hopefully a more simple example)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×