The compiler complains that resultingThing
in the code below is being used before being assigned to.
private IEnumerable<IThing> FindThings(dynamic spec)
{
if (spec == null)
yield break;
IThing resultingThing;
if (spec.Something > 0 && dictionary.TryGetValue(spec.Something, out resultingThing))
yield return resultingThing;
else
// ...
}
Why does it claim this?
I have tried a different version of the method in which there are no yield usages (e.g. just return IEnumerable<IThing>
) but with the dynamic parameter, and I have tried a version of the method in which dynamic is not passed in (i.e. what we've done in prior versions of C#). These compile.
resultingThing
in theelse
case? – DroveresultingThing
in theelse
. Also, initializing it tonull
gets rid of the compiler error as expected. – AnchetaresultingThing
using thedefault
keyword, i.e.default(IThing)
. For reference types, this should benull
; for value types, it should be the equivalent of a default-constructed type. – Marbleize