0

In the following piece of code, the compiler gives a "Use of unassigned local variable" on 'intValue' on the return line. However, there is no case where "intValue > 500" will be reached where it will be unassigned (because if intValue is unassigned, then valueIsInt is false, and the statement returns false before reaching intValue)

Is there a way to get around this issue without modifying the logic or business logic of the code? This is a very simplified example; in a case where intValue is another type and the condition intValue > 500 is more complex, we can't simply give intValue a value in the else block like intValue = 0

bool valueIsInt;

if (value is int intValue)
{
    valueIsInt = true;
}
else
{
    valueIsInt = false;
}

return valueIsInt && intValue > 500;

I want to avoid this in case the code in the else statement is more complex:

else
{
    return false;
}

Such as this:

bool valueIsInt;

if (value is int intValue)
{
    valueIsInt = true;
}
else
{
    if (value is string stringValue)
        valueIsInt = int.TryParse(stringValue, out intValue);
    else
        return false;
}

return valueIsInt && intValue > 500;
DMX David Cardinal
  • 599
  • 2
  • 7
  • 19
  • 1
    just set an initial value? bool valueIsInt = false; – maembe Jul 29 '20 at 22:08
  • 2
    You do realize you can write it as `return value is int intValue && intValue > 500;`, right? I mean, I know you wrote the sample is simplified buy if it resembles your code in any way you can probably re-write at least parts of it – Zohar Peled Jul 29 '20 at 22:11
  • See [this answer](https://stackoverflow.com/a/8933935/43846) - _"Some variables are not classified as initially assigned; local variables in particular are not initially assigned. They must be classified by the compiler as **"definitely assigned"** at all points where their values are used."_ and further on _"**The reason this is illegal in C# is because using an unassigned local has high likelihood of being a bug**. We simply make it illegal, and then the compiler prevents you from ever having such a bug"_ – stuartd Jul 29 '20 at 22:15
  • Not exactly a duplicate, but here's a [question asking why this happens.](https://stackoverflow.com/q/62978951/3094533) – Zohar Peled Jul 29 '20 at 22:21
  • For clarification, in response to these comments: I know the snippet of code can be simplified, that is not the question, and although I thank you for your kind intentions, the suggestions don't answer my real question. I have added clarifications in the question iteslef. – DMX David Cardinal Jul 30 '20 at 16:03

1 Answers1

1

Updated

static bool IsValueGreaterThan500(object value)
{
    try
    {
        // Converts the value to int if it is not already an int
        // from bool, short, float, double, string, etc.
        return Convert.ToInt32(value) > 500;
    }
    catch
    {
        return false;
    }
}
TimTIM Wong
  • 788
  • 5
  • 16
  • 1
    What does TypeScript have to do with this question at all...? Also it is most certainly not smarter, it would have the same problem in this snippet. TypeScript would be *more* verbose as pattern matching isn't a thing. – Klaycon Jul 29 '20 at 22:31
  • I don't know what TypeScript has to do with it, but the c# part of this answer is solid :-) – Zohar Peled Jul 29 '20 at 22:37
  • TypeScript further distinguish true and false as different types during static type analysis of every branch such that the compiler would know whether the expression after && may be evaluated, and thus knows intValue must be assigned if valueIsInt is true. Though you're right, there is no pattern matching. – TimTIM Wong Jul 29 '20 at 22:38
  • @TimTimWong I converted the asker's code in the TypeScript playground and did not observe the behavior you describe. I get "Variable 'intValue' is used before being assigned." What you're describing sounds rather fantastical. – Klaycon Jul 29 '20 at 22:40
  • Oops, I was confused how type narrowing and type guards work in TypeScript. It only applies inside the scope of the conditional branch. – TimTIM Wong Jul 29 '20 at 22:50
  • This answer doesn't answer the question, it offers a working version of the code I wrote (only as an example). I have added more detail in the question to clarify what kind of answer is expected. – DMX David Cardinal Jul 30 '20 at 16:02
  • Do you mean to compare the numeric value whenever possible? See my updated answer. – TimTIM Wong Jul 30 '20 at 19:41