I thought I couldn't shadow...
-
I was reading in Wikipedia (don't say it) about how C# is one of the languages in which you can't use variable shadowing. The article gives an example of shadowing in C++ (in which you CAN shadow variables). However, I have been unaware of this limitation (and I think I may have used variable shadowing in the past). Just to be sure, I typed up a similar bit of code in C#, and it compiled and ran just fine:
using System;
public class test
{
public static int testing = 0;public static void Main() { int testing = 1; Console.WriteLine(testing); Console.WriteLine(test.testing); Console.ReadLine(); }
}
// console output is:
// 1
// 0So, how is it NOT variable shadowing, if it's the same construction as the C++ example? Is it just that they renamed it something else (like "hiding")?
-Daniel Typing too fast fro my owngood
-
I was reading in Wikipedia (don't say it) about how C# is one of the languages in which you can't use variable shadowing. The article gives an example of shadowing in C++ (in which you CAN shadow variables). However, I have been unaware of this limitation (and I think I may have used variable shadowing in the past). Just to be sure, I typed up a similar bit of code in C#, and it compiled and ran just fine:
using System;
public class test
{
public static int testing = 0;public static void Main() { int testing = 1; Console.WriteLine(testing); Console.WriteLine(test.testing); Console.ReadLine(); }
}
// console output is:
// 1
// 0So, how is it NOT variable shadowing, if it's the same construction as the C++ example? Is it just that they renamed it something else (like "hiding")?
-Daniel Typing too fast fro my owngood
-
However, this will not compile:
private static void doSomething()
{
int number = 5;if (true) { int number = 6; }
}
Even though that DOES compile in C++, it's not quite what I was expecting. From different sites (google "variable shadowing"), variable shadowing always refers to when a variable shares its name with that of a different class, method, or struct, not if-block scope. I agree that if-block scope is still just another level of scope (deeper than method or class scope), but it's not specified in different definitions of variable shadowing (only class/struct and method scopes are mentioned, not if-blocks). Thank you for bringing that to my attention, I hadn't though of if-blocks. We'll see how long it lasts if I add that detail to the Wikipedia article...
-Daniel Typing too fast fro my owngood
-
I was reading in Wikipedia (don't say it) about how C# is one of the languages in which you can't use variable shadowing. The article gives an example of shadowing in C++ (in which you CAN shadow variables). However, I have been unaware of this limitation (and I think I may have used variable shadowing in the past). Just to be sure, I typed up a similar bit of code in C#, and it compiled and ran just fine:
using System;
public class test
{
public static int testing = 0;public static void Main() { int testing = 1; Console.WriteLine(testing); Console.WriteLine(test.testing); Console.ReadLine(); }
}
// console output is:
// 1
// 0So, how is it NOT variable shadowing, if it's the same construction as the C++ example? Is it just that they renamed it something else (like "hiding")?
-Daniel Typing too fast fro my owngood
likefood wrote:
Is it just that they renamed it something else (like "hiding")?
In C++, the following code is perfectly valid
int v = 0;
if(true)
{
int v = 10;
std::cout << v; // prints 10
}
std::cout << v; // prints 0If you try this in C#, you will get compiler error. However, this is not called as hiding in C#. Hiding is something like this
class Base
{
public string SomeVar = string.Empty;
}
class Derived : Base
{
public string SomeVar = string.Empty; // hides Base.SomeVar
}:)
Navaneeth How to use google | Ask smart questions
-
I was reading in Wikipedia (don't say it) about how C# is one of the languages in which you can't use variable shadowing. The article gives an example of shadowing in C++ (in which you CAN shadow variables). However, I have been unaware of this limitation (and I think I may have used variable shadowing in the past). Just to be sure, I typed up a similar bit of code in C#, and it compiled and ran just fine:
using System;
public class test
{
public static int testing = 0;public static void Main() { int testing = 1; Console.WriteLine(testing); Console.WriteLine(test.testing); Console.ReadLine(); }
}
// console output is:
// 1
// 0So, how is it NOT variable shadowing, if it's the same construction as the C++ example? Is it just that they renamed it something else (like "hiding")?
-Daniel Typing too fast fro my owngood
It appears that C# does not allow shadowing from within a decision block. It has already been clarified that it won't work between inside and if-block and its outer method. And I already knew you can't shadow between case statements in a switch block (when you can with Java). However, it has no problem with it between a method and its class (or between an inner and an outer class). It would be cool if that distinction was made more clear in the documentation. Thanks for pointing out the decision-block scenario to me, StephenWhitfield and Navaneeth!
-Daniel Typing too fast fro my owngood
-
It appears that C# does not allow shadowing from within a decision block. It has already been clarified that it won't work between inside and if-block and its outer method. And I already knew you can't shadow between case statements in a switch block (when you can with Java). However, it has no problem with it between a method and its class (or between an inner and an outer class). It would be cool if that distinction was made more clear in the documentation. Thanks for pointing out the decision-block scenario to me, StephenWhitfield and Navaneeth!
-Daniel Typing too fast fro my owngood
likefood wrote:
And I already knew you can't shadow between case statements in a switch block (when you can with Java)
To know why C# doesn't support this, write a C# code with switch case, compile it and look into the generated IL. You will see the reason :)
Navaneeth How to use google | Ask smart questions
-
likefood wrote:
And I already knew you can't shadow between case statements in a switch block (when you can with Java)
To know why C# doesn't support this, write a C# code with switch case, compile it and look into the generated IL. You will see the reason :)
Navaneeth How to use google | Ask smart questions
-
Even though that DOES compile in C++, it's not quite what I was expecting. From different sites (google "variable shadowing"), variable shadowing always refers to when a variable shares its name with that of a different class, method, or struct, not if-block scope. I agree that if-block scope is still just another level of scope (deeper than method or class scope), but it's not specified in different definitions of variable shadowing (only class/struct and method scopes are mentioned, not if-blocks). Thank you for bringing that to my attention, I hadn't though of if-blocks. We'll see how long it lasts if I add that detail to the Wikipedia article...
-Daniel Typing too fast fro my owngood