Home > Mobile >  Local variable initialized inside 'if' is still available after that 'if' in C#
Local variable initialized inside 'if' is still available after that 'if' in C#

Time:05-11

Have not found an answer to this question yet (proboply because I am looking wrong for it). I was just wondering following. Why is it not possible to have this code:

if (obj is string str) {
    // do stuff
}
if (obj is string str) {
    // do stuff
}

This would throw following error: Error CS0128 A local variable or function named 'str' is already defined in this scope

I know I could use this instead:

{
    if (obj is string str) {
        // do stuff
    }
}
{
    if (obj is string str) {
        // do stuff
    }
}

But it has other disadvantages, the biggest beeing more lines of code for me.

I would guess its because the compiler actually converts the code (the upper of the two) to following:

string str = obj as string;
if (str != null) {
    // do stuff
}

Or something simillar.

If this is the case I wonder why and if there may already be a better way than writing:

if (obj is string str1) {
    // do stuff
}
if (obj is string str2) {
    // do stuff
}
if (obj is string str3) {
    // do stuff
}

Hope someone can tell me what the problems in C# Logic are and a way to deal with it. If a Question like this already exists please link it I really could not find one.

Edit: Because the usecase of this was asked. First of I want to know the logic behind that in general but secondly I have an usecase for this right now which will not be possible to hanndle differently because of some reasons.

It would be useful in a case where I have e.g. different data that is passed and I do the type-specific operation1 then something that both need and then another operation that is type-specific.

CodePudding user response:

The question is, why is the error: "Error CS0128 A local variable or function named 'str' is already defined in this scope" .

The answer is, The code if (obj is string str1) is syntactic sugar for declaring a variable and assigning it.

I used sharplab.io for this:

public class C 
{
    public void M() 
    {
        object obj = "Test string";
        
        if (obj is string str1)
        {
            Console.WriteLine($"{obj} is a string");
        }
    }
}

Is compiled to:

public class C
{
    public void M()
    {
        object obj = "Test string";
        string text = obj as string;
        if (text != null)
        {
            Console.WriteLine(string.Format("{0} is a string", obj));
        }
    }
}

So the obj variable is NOT declared in the inner scope but in the outer scope.

CodePudding user response:

Just to suggest an alternative way to declare things to avoid the compile error:

You can declare the variable outside the conditions, and then use it like so:

string? str;

if ((str = obj as string) != null)
{
    // do stuff
}
if ((str = obj as string) != null)
{
    // do stuff
}

It's not a lot better, but you might prefer it.

  • Related