none
iOutValue of Dictionary Class in C#.Net RRS feed

  • Question

  • Issue : Difference in behaviour of dictionary class in C# .NET

    Description : In the code snippet given below, In .NET 3.5 version, we got -1 in iOutValue variable if the value doesn't exist in Dictionary. But with .NET 4.5, we are getting 0 in iOutValue variable if the value doesn't exist in the Dictionary. 

    Code Snippet:

    Dictionary<Int32, Int32> dicobj = new Dictionary<Int32, Int32>;
    Int32 iOutValue = -1; 
    dicobj.TryGetValue(0, out iOutValue);
    Console.WriteLine("Out Value : " + iOutValue);

     
    Wednesday, July 19, 2017 12:24 PM

Answers

  • I could not reproduce the -1 output using .NET 3.5. For me it set it to 0 in .NET 3.5 and .NET 4.5

    But assigning a value to a variable about to be passed in to an 'out' parameter is pointless - out parameters mean that the method always passes something back, even if that value is meaningless in the context (which is the case here when a key does not exist in a dictionary). This would overwrite anything you passed in.

    You should be checking the return value of TryGetValue method to determine whether the key exists in the dictionary:

    if (dicobj.TryGetValue(0, out iOutValue))
    {
       // use iOutValue
    }

    Note I said "key" does not exists. It is the value associated with the key - the first parameter - you are looking for. Not the value itself which is returned, if found, in the 'out' parameter.


    • Edited by RJP1973 Wednesday, July 19, 2017 1:06 PM
    • Marked as answer by singh.neetavns Friday, July 21, 2017 6:14 PM
    Wednesday, July 19, 2017 12:53 PM
  • The behaviour is mentioned in the documentation for TryGetValue, and you can also see the line ‘value = default(TValue)’ in the modern sources:

    Wednesday, July 19, 2017 5:37 PM

All replies

  • I could not reproduce the -1 output using .NET 3.5. For me it set it to 0 in .NET 3.5 and .NET 4.5

    But assigning a value to a variable about to be passed in to an 'out' parameter is pointless - out parameters mean that the method always passes something back, even if that value is meaningless in the context (which is the case here when a key does not exist in a dictionary). This would overwrite anything you passed in.

    You should be checking the return value of TryGetValue method to determine whether the key exists in the dictionary:

    if (dicobj.TryGetValue(0, out iOutValue))
    {
       // use iOutValue
    }

    Note I said "key" does not exists. It is the value associated with the key - the first parameter - you are looking for. Not the value itself which is returned, if found, in the 'out' parameter.


    • Edited by RJP1973 Wednesday, July 19, 2017 1:06 PM
    • Marked as answer by singh.neetavns Friday, July 21, 2017 6:14 PM
    Wednesday, July 19, 2017 12:53 PM
  • Thank you for the response.

    Yes, we had realized that too and corrected at our end to read the return value instead of iOutValue.

    However, need to understand the behavior of the iOutValue for a given scenario.  If everything remains same, should not the iOuitValue return the same value irrespective of the .Net framework. But you mentioned it is actually returning the same value  at your end in both the .Net environments. 

    Will validate once again at our and update back.

    Wednesday, July 19, 2017 3:38 PM
  • The behaviour is mentioned in the documentation for TryGetValue, and you can also see the line ‘value = default(TValue)’ in the modern sources:

    Wednesday, July 19, 2017 5:37 PM