Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading