Domanda

If i have a concurrent dictionary, and i try a TryGetValue, and i test if that fails i do stuff, but if it doesn't fail, and the out value retrieved from the TryGetValuefunction is equal to what is was before i tried the TryGetValue, i do something else.

My Question is, (assuming nothing in my ConcurrentDicationary will ever be set to default(DateTime)), will my second if statement ever actually execute? or is it impossible given the current situation?

var m_unitsWaitingForReadResponse = new ConcurrentDictionary<string, DateTime>();
DateTime outVal = default(DateTime);
if (!m_unitsWaitingForReadResponse.TryGetValue("teststring", out outVal))
{
    //Do Stuff
}
if (outVal == default(DateTime))
{
    //Do Stuff 2
}
È stato utile?

Soluzione 2

The MSDN documentation states that TryGetValue will return default(TValue) if the key doesn't exist in the dictionary. So yes, it should execute.

You can test the return value of TryGetValue instead by simply using an else clause on the first if, like this:

m_unitsWaitingForReadResponse= new ConcurrentDictionary<string, DateTime>();

DateTime outVal = default(DateTime);

if (!(m_unitsWaitingForReadResponse.TryGetValue("teststring", out outVal)))
{
    //Do Stuff
}
else
{
    //Do Stuff 2
}

I assume that your ConcurrentDictionary object will contain data at some point, using code not seen here.

Altri suggerimenti

The second if block will always execute if a value for the provided key isn't in the ConcurrentDictionary.

Read the Parameters section for the Value at this URL

ConcurrentDictionary.TryGetValue Method

TryGetValue will always return false, because you haven't put anything into the dictionary. After it returns false, the first if block will not run, but the second if block will run, both if blocks will run, because outVal will have the value of default(DateTime).

If, however, the dictionary has some data in it, it's possible for default(DateTime) to be the value associated with the key you pass to TryGetValue. In that case, the first if block won't run, but the second if block will run.

EDIT:

The original version of the first paragraph above is incorrect. Notice the negation (!) in your if statement:

if (!m_unitsWaitingForReadResponse.TryGetValue("teststring", out outVal))

That expression evaluates to true when TryGetValue returns false. Therefore, you have the following possibilities, though by your assumption, possibility 3 does not apply in your case:

  1. The key is not present in the dictionary
  2. The value associated with the key in the dictionary is not equal to default(DateTime)
  3. The value associated with the key in the dictionary is equal to default(DateTime)

In the first case, TryGetValue returns false; Do Stuff executes; outVal is equal to default(DateTime); and Do Stuff 2 executes.

In the second case, TryGetValue returns true; Do Stuff does not execute; outVal is not equal to default(DateTime); and Do Stuff 2 does not execute.

In the third (impossible) case, TryGetValue returns true; Do Stuff does not execute; outVal is equal to default(DateTime); and Do Stuff 2 executes.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top