-
As you can see in this example, none of the ways that make sense to me work for getting the possible value out from the MathContext.Calculate()'s out argument. I shouldn't have to use the ! operator right? if i use ? it says cthird is now unassigned and spouts an error. which, it should be either null, or atleast allow you to accompany that out with a null. here's how i see it should look. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
You should be able to use the Example signature:
Full example: |
Beta Was this translation helpful? Give feedback.
-
You appear to be battling with definite assignment, not nullability. Assuming that If |
Beta Was this translation helpful? Give feedback.
-
Due to null short-circuiting this behavior is expected. If But since you already initialized // This causes CS0165 because cthird will not be assigned if mathContext is null:
//Token third2 = Token.Invalid;
//tokens[2].mathContext?.Calculate(out Token cthird);
//third2 = cthird; // issue: cthird might be uninitialized here
// But you can simply do this:
Token third2 = Token.Invalid;
tokens[2].mathContext?.Calculate(out third2); // third2 is now assigned even if Calculate didn't execute ps: and please avoid posting code as image because it's harder for us to answer if we cannot copy-paste it. |
Beta Was this translation helpful? Give feedback.
Due to null short-circuiting this behavior is expected. If
tokens[2].mathContext
isnull
, thenCalculate
is not executed so yourcthird
remains unassigned. You even highlighted CS0165, which explains the issue.But since you already initialized
third2
there is no need to introduce another variable in the out parameter and then assign it tothird2
: