I created a c# test class like below, I intend to test what gonna happen if I created one variable twice in and out of scope. But to my surprise, it runs and outputs the value "inside".
This is an illegal way to multiple declare a same variable if I wrote this code snippet into Main() method, the compiler error would occur accordingly. But how come it can work in a separate class? Then how can we avoid making such mistakes?
class Program
{
static void Main(string[] args)
{
Test tst = new Test();
tst.ScopeAlert();
Console.ReadLine();
}
}
public class Test
{
string scope = "outside";
public void ScopeAlert()
{
string scope = "inside";
Console.WriteLine(scope);
}
}