Question

I've got the following code to compose a simple SOAP Post form data value:

     var postParameters = new Dictionary<string, string>
        {
           { "someNumber", "100" },
           { "someString", "Hello World" }
        };

     var resultWhenNotInConditional = 
        postParameters
           .Keys
           .Zip(postParameters.Values, 
               (key, value) => string.Format("{0}={1}", key, value))
           .Aggregate<string, string>(null,
              (prev, next) =>
              (prev != null)
                 ? string.Format("{0}&{1}", prev, next)
                 : next);

which works as designed, viz

resultWhenNotInConditional = "someNumber=100&someString=Hello World"

However, when I wrap this in a conditional operator to do a null check, like so:

     var resultWhenInConditional = 
        (postParameters != null)
        ? postParameters
           .Keys
           .Zip(postParameters.Values, 
                (key, value) => string.Format("{0}={1}", key, value))
           .Aggregate<string, string>(null,
              (prev, next) =>
              (prev != null)
                 ? string.Format("{0}&{1}", prev, next)
                 : next)
        : string.Empty;

The resultWhenInConditional seems to be always null, irrespective of whether postParameters is set to null or to a valid Dictionary<string, string>. (Changing the var to an explicit string also has no effect).

The only way I can fix this is by adding ToString() after the Aggregate, viz:

     var resultWhenInConditional = 
       (postParameters != null)
        ? postParameters
           .Keys
           .Zip(postParameters.Values, 
                (key, value) => string.Format("{0}={1}", key, value))
           .Aggregate<string, string>(null,
              (prev, next) =>
              (prev != null)
                 ? string.Format("{0}&{1}", prev, next)
                 : next)
           .ToString() // R# warns this is redundant
        : string.Empty;

So my question is, why do I need to add the additional .ToString() when inside the conditional operator?

  • Microsoft Visual Studio Professional 2010 Version 10.0.40219.1 SP1Rel
  • Microsoft .NET Framework Version 4.0.30319 SP1Rel

Edit

Thanks for the feedback!

To confirm that this is just an aberration - the VS IDE is reporting the variable as NULL (in the mouse hover + Immediate Window), but only when debugging the unit tests individually with the R# NUnit test runner. Debugging under the console App reports the value correctly in the IDE.

i.e. This only happens when debugging, under the Resharper NUnit TestRunner.

As soon as the variable is accessed by further code (e.g. Assert / Console.Writeline etc), it is clear that the value is NOT actually null.

I've added a console app to GitHub and a screenshot here

None of the unit tests actually fail, i.e. the value isn't actually null :-)

  • R# 7.1.3 C# Edition (2224)
  • NUnit 2.6.1.12217
Était-ce utile?

La solution

To confirm that this is just an aberration - the VS IDE is reporting the variable as NULL (in the mouse hover + Immediate Window), but only when debugging the unit tests individually with the R# NUnit test runner. Debugging under the console App reports the value correctly in the IDE.

i.e. This only happens when debugging, under the Resharper NUnit TestRunner.

As soon as the variable is accessed by further code (e.g. Assert / Console.Writeline etc), it is clear that the value is NOT actually null.

I've added a console app to GitHub and a screenshot here

None of the unit tests actually fail, i.e. the value isn't actually null :-)

  • R# 7.1.3 C# Edition (2224)
  • NUnit 2.6.1.12217
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top