Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I thought I couldn't shadow...

I thought I couldn't shadow...

Scheduled Pinned Locked Moved C#
csharpc++testingbeta-testingtutorial
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    likefood
    wrote on last edited by
    #1

    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
    // 0

    So, 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

    S N L 3 Replies Last reply
    0
    • L likefood

      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
      // 0

      So, 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

      S Offline
      S Offline
      Steve_
      wrote on last edited by
      #2

      However, this will not compile:

      private static void doSomething()
      {
      int number = 5;

      if (true)
      {
          int number = 6;
      }
      

      }

      L 1 Reply Last reply
      0
      • S Steve_

        However, this will not compile:

        private static void doSomething()
        {
        int number = 5;

        if (true)
        {
            int number = 6;
        }
        

        }

        L Offline
        L Offline
        likefood
        wrote on last edited by
        #3

        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

        S 1 Reply Last reply
        0
        • L likefood

          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
          // 0

          So, 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

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          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 0

          If 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

          1 Reply Last reply
          0
          • L likefood

            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
            // 0

            So, 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

            L Offline
            L Offline
            likefood
            wrote on last edited by
            #5

            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

            N 1 Reply Last reply
            0
            • L likefood

              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

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              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

              L 1 Reply Last reply
              0
              • N N a v a n e e t h

                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

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                That is no reason, they could rename the new variable to, say, CS$0$0037 they do stuff like that all the time.. edit: quick example: if you switch on a function parameter instead of a local (a new local will be created with a funky name)

                1 Reply Last reply
                0
                • L likefood

                  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

                  S Offline
                  S Offline
                  Steve_
                  wrote on last edited by
                  #8

                  In my experience, probably not long, then again, perhaps I am just always wrong :D.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups