Questions tagged [trygetvalue]

"TryGetValue" usually refers to an operation of value lookup at dictionary (or map) by a specified key. "Try" here means that it is a failsafe method, i.e. in case of the key is not contained by the dictionary, some indicator of missing value is returned.

When tagging a question with , be sure to tag it with the language being used, and with the related data structure, like , , , , , , or .

In C++

std::map<Key, T, Compare, Allocator>::find: returns an iterator, which is empty in case of no such key.

In .NET

Dictionary<TKey, TValue>.TryGetValue: returns true or false depending on whether the provided key was found and a value as an out parameter.

In Python

dict.get: Returns the value, which is substituted by a provided default in case of no such a key.

In Java

Map<K, V>.get: Returns the value or null if this map contains no mapping for the key.


For questions about Mapping Functions over collections of data, Please Use tag.

56 questions
12
votes
3 answers

Is there any implementation to Remove by Key and get the Value at the same time?

I'm doing a performance critical program (little academic stuff) and I'm looking to optimize wherever possible (not like it proved "this is the" bottleneck). I have a custom dictionary structure (a wrapper around .NET Dictionary<,>) and I would…
nawfal
  • 70,104
  • 56
  • 326
  • 368
11
votes
2 answers

Initialize tuple with empty or null values in C#

I have this dictionary and tuples set up in SetValue() as below :- var myDict = new Dictionary>(); private void SetValue() { var myTuple1= Tuple.Create("ABC", "123"); var myTuple2= Tuple.Create("DEF", "456"); …
rajibdotnet
  • 1,498
  • 2
  • 17
  • 29
9
votes
3 answers

Dictionary TryGetValue with int values, how to avoid double lookup

When doing something like: int value; if (dict.TryGetValue(key, out value)) { if (condition) { //value = 0; this copies by value so it doesn't change the existing value dict[key] = 0; } } else { dict[key] = 0; } Is…
tmakino
  • 618
  • 1
  • 5
  • 20
7
votes
3 answers

Using TryGetValue() in LINQ?

This code works, but is inefficient because it double-lookups the ignored dictionary. How can I use the dictionary TryGetValue() method in the LINQ statement to make it more efficient? IDictionary records = ... IDictionary
Joe Daley
  • 45,356
  • 15
  • 65
  • 64
6
votes
10 answers

How can I make this Dictionary TryGetValue code more readable?

I'd like to test if an id was not yet known or, if it is known, if the associated value has changed. I'm currently using code similar to this, but it is hard to understand for those not familiar with the pattern. Can you think of a way to make it…
mafu
  • 31,798
  • 42
  • 154
  • 247
3
votes
1 answer

C# Dictionary ContainsKey/TryGetValue not working

I've looked around for a while and seen plenty of references to modifying GetHashCode() and things when playing with ContainsKey() and TryGetValue() - but all those issues and examples have all been with some obscure user-specific key. I have a…
Krenom
  • 1,894
  • 1
  • 13
  • 20
2
votes
2 answers

c#: Dictionary TryGetValue creating instance instead of getting reference

I'm a total noob with c#, and cannot figure out why the same method works in different ways. I'm making a simple spreadsheet application, and am using a Dictionary of cells, where the key is a string name and the value is a Cell object: public…
2
votes
2 answers

TryGetValue Evaluating to False Even When Key is Present

This question is basically the same as this one, although the answer to that person's problem turned out to be a simple trailing space. My issue is that I'm retrieving data from a web API as dictionary and then trying get the values out of it. I'm…
Philip Stratford
  • 4,513
  • 4
  • 45
  • 71
2
votes
1 answer

Alternatives to bool TryGetX(out X x) pattern

I often use the pattern bool TryGetX(out X x) when I need to get data from a service that may fail. I use this when neither the client nor the service can know if the data is available until trying to retrieve it. I've never been totally happy…
GreenRibbon
  • 299
  • 2
  • 16
2
votes
2 answers

Get value into HTML web resource in Dynamics CRM Online

I am trying to get a value from Dynamics CRM into a HTML webresource. I have found this and tried to make the code out of it: https://msdn.microsoft.com/en-us/library/jj602964(v=crm.7).aspx It says to use var nameValue =…
2
votes
2 answers

GetValueOrDefault or null-coalescing for Dicitionary or List

I'm trying to get some values from a list. But I want to assure that if key doesn't exist, it will return a default value 0 instead of throwing exception. var businessDays = days.Where(x => x.Year == year).ToDictionary(x => x.Month, x =>…
Andre Figueiredo
  • 12,930
  • 8
  • 48
  • 74
2
votes
1 answer

Is TryGetValue thread safe with itself

So, I've got a Dictionary Foo In one thread I have: void Thread1() { ... if (!Foo.TryGetValue(key, out v)) { Foo.Add(new VType()); } ... } The only time Foo is ever accessed by another thread is by a TryGetValue. So, how…
Jay
  • 983
  • 2
  • 8
  • 23
2
votes
1 answer

Not sure why I'm getting an InvalidCastException

I am getting an InvalidCastException and I don't understand why. Here is the code that raises the exception: public static void AddToTriedList(string recipeID) { IList triedIDList = new ObservableCollection(); try { …
maxdelia
  • 858
  • 2
  • 14
  • 35
1
vote
5 answers

Why TryGetValue deallocates my Dictionary ?

I have this piece of code : Dictionary tempDict = new Dictionary(); if(xDicionary.TryGetValue(...., out tempDict) { tempDict.Add(...); } else { tempDict.Add(..); } If the code passes to the else block then I got…
Patryk
  • 22,602
  • 44
  • 128
  • 244
1
vote
2 answers

TryGetValue on a null dictionary

I am trying to use TryGetValue on a Dictionary as usual, like this code below: Response.Context.Skills[MAIN_SKILL].UserDefined.TryGetValue("action", out var actionObj) My problem is the dictionary itself might be null. I could simply use a "?."…
Alexandre Paiva
  • 304
  • 3
  • 13
1
2 3 4