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. The Lounge
  3. Hey CG, C# is pretty weird too

Hey CG, C# is pretty weird too

Scheduled Pinned Locked Moved The Lounge
csharpc++comarchitecturehelp
28 Posts 15 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.
  • N Nish Nishant

    Nemanja Trifunovic wrote: Yep, that's in accordance with the C# Language Specifcation - 3.3 Declarations[^] I think the wording is quite confusing. It says :- The scope of a local variable declared in a local-variable-declaration is the block in which the declaration occurs. And it defines block as :- A block consists of an optional statement-list (Section 8.2.1), enclosed in braces. If the statement list is omitted, the block is said to be empty. A block may contain declaration statements (Section 8.5). The scope of a local variable or constant declared in a block is the block. So in this code :-

    using System;

    class B
    {
    //start function-block
    public static void Main()
    {
    //start for-block
    for(int i=0; i<10; i++)
    {
    B b = new B(); // this is in the for-block
    }
    //stop for-block

    	B b = new B(); //this is in the function-block
    }
    //stop function-block
    

    }

    I don't see any reason why the 2nd declaration of b should give a compiler error :-(


    My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com

    R Offline
    R Offline
    Russell Morris
    wrote on last edited by
    #17

    What the C# language specification is saying is that the scope of the variable 'b' declared in the primary block for Main(), just after the for() loop, is in fact the entire Main() block, not just the part of that block after the for loop. The code you've posted is equivalent to the following code:

    class B
    {
      //start function block
      public static void Main()
      {
        B b = null;

    //start for-block
        for(int i=0;i<10;i++)
        {
          B b = new B();
        }

    b = new B();
      }
    }

    In essence, any variable declarations you make in a block, regardless of where you make them in the block, are treated as if those declarations were made at the very beginning of the block itself. This is, I believe, in contrast with standard C and C++ convention, where a local variable's scope is the portion of the block in which it is declared after the declaration. I seem to remember that prior standards for C (maybe C++ as well, I'm not sure...) insisted that all local variables be declared at the beginning of the block to which they belonged. I don't believe that is the case any longer, but I could be wrong. -- Russell Morris "So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy

    1 Reply Last reply
    0
    • N Nish Nishant

      using System;

      class B
      {
      public static void Main()
      {
      if(true)
      {
      B b = new B();
      }

      	B b = new B(); // <-- throws error CS0136
      }
      

      }

      error CS0136: A local variable named 'b' cannot be declared in this scope because it would give a different meaning to 'b', which is already used in a 'child' scope to denote something else :wtf: :wtf: :wtf:


      My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com

      T Offline
      T Offline
      Tim Ranker
      wrote on last edited by
      #18

      Could it be that if(true) is being optimized out by the compiler? What happens if true is replaced by a non-const variable? Kind regards, Tim

      J 1 Reply Last reply
      0
      • N Nish Nishant

        Alright, I can see how this is probably an attempt to enforce good coding/naming practices, but it should only have been a warning. Throwing a compiler error is outright dumb! Nish


        My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com

        R Offline
        R Offline
        Richard Stringer
        wrote on last edited by
        #19

        Why warnings - a real coder always treats warnings as errors - right ? Richard "Under certain circumstances, profanity provides a relief denied even to prayer --Mark Twain (1835 - 1910)

        1 Reply Last reply
        0
        • M Marc Clifton

          Uh, the "throws error" really got me confused there for a minute, thinking that the runtime threw an exception. As compiler errors go, this one is both annoying, coming from C++, and has saved my butt several times. One thing that I thought was interesting that I didn't see anyone mentioning in the VB.NET vs. C# threads is that C# is a standard language while VB.NET is basically just whatever Microsoft wants it to be (this is true, right?). As a result, C# is being ported to other platforms. Is VB.NET getting generics? Are there any open source VB.NET projectscompilers? I haven't heard of any developments along these lines. Just one more comment--there are a couple VB.NET to C# converters out there. I haven't seen any C# to VB.NET converters. Why is that? (Besides that I haven't bothered actually looking). Marc MyXaml Advanced Unit Testing

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #20

          Marc Clifton wrote: Are there any open source VB.NET projectscompilers? Yes, MonoBasic[^].

          Any remotely useful information on my blog will be removed immediately.

          There are 10 kinds of people in the world. Those who have heard of the ubiquitous, overused, worn-out-like-an-old-shoe binary "joke" and those who haven't. Judah Himango

          1 Reply Last reply
          0
          • L leppie

            Dont be so lazy!

            using System;

            class B
            {
            public static void Main()
            {
            if(true)
            {
            B b = new B();
            }
            { B b = new B(); } // <-- throws no error CS0136
            }
            }

            top secret
            Download xacc-ide 0.0.3 now!
            See some screenshots

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #21

            According to section 3.3 of the C# Language Specification "The local variable declaration space of a block includes any nested blocks." That's why Nish's code didn't compile. Your code declares instances of B in two separate blocks, so it compiles fine.

            J 1 Reply Last reply
            0
            • N Nish Nishant

              using System;

              class B
              {
              public static void Main()
              {
              if(true)
              {
              B b = new B();
              }

              	B b = new B(); // <-- throws error CS0136
              }
              

              }

              error CS0136: A local variable named 'b' cannot be declared in this scope because it would give a different meaning to 'b', which is already used in a 'child' scope to denote something else :wtf: :wtf: :wtf:


              My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com

              J Offline
              J Offline
              Jorgen Sigvardsson
              wrote on last edited by
              #22

              I noticed that one too. Fascist compiler. :mad: -- Weiter, weiter, ins verderben. Wir müssen leben bis wir sterben. I blog too now[^]

              1 Reply Last reply
              0
              • T Tim Ranker

                Could it be that if(true) is being optimized out by the compiler? What happens if true is replaced by a non-const variable? Kind regards, Tim

                J Offline
                J Offline
                Jorgen Sigvardsson
                wrote on last edited by
                #23

                The symbol tables are built far earlier in the compiling process than optimization. In fact, optimization of some piece of code cannot occur until all semantic question marks pertaining to that code have been resolved. -- Weiter, weiter, ins verderben. Wir müssen leben bis wir sterben. I blog too now[^]

                1 Reply Last reply
                0
                • S S Senthil Kumar

                  According to section 3.3 of the C# Language Specification "The local variable declaration space of a block includes any nested blocks." That's why Nish's code didn't compile. Your code declares instances of B in two separate blocks, so it compiles fine.

                  J Offline
                  J Offline
                  Jorgen Sigvardsson
                  wrote on last edited by
                  #24

                  That's just silly since you cannot access variables declared in a nested scope. It's counter intuitive for, dare I say, all programmers. Name one language which has similar scoping rules? -- Weiter, weiter, ins verderben. Wir müssen leben bis wir sterben. I blog too now[^]

                  S 1 Reply Last reply
                  0
                  • J Jorgen Sigvardsson

                    That's just silly since you cannot access variables declared in a nested scope. It's counter intuitive for, dare I say, all programmers. Name one language which has similar scoping rules? -- Weiter, weiter, ins verderben. Wir müssen leben bis wir sterben. I blog too now[^]

                    S Offline
                    S Offline
                    S Senthil Kumar
                    wrote on last edited by
                    #25

                    It's silly if you look at it that way. But this does help in some cases (see Marc's post above) where it prevents redeclaration of a variable with a same name in a nested scope. I've done that quite a few times and spent a considerable time debugging the problem. Regards Senthil

                    J 1 Reply Last reply
                    0
                    • S S Senthil Kumar

                      It's silly if you look at it that way. But this does help in some cases (see Marc's post above) where it prevents redeclaration of a variable with a same name in a nested scope. I've done that quite a few times and spent a considerable time debugging the problem. Regards Senthil

                      J Offline
                      J Offline
                      Jorgen Sigvardsson
                      wrote on last edited by
                      #26

                      S. Senthil Kumar wrote: where it prevents redeclaration of a variable with a same name in a nested scope Yes, in the "forward" direction I can see the point. But it also blocks in the "backward" direction, which is silly since the outer/after scope cannot reference the variable declared in the inner scope. Oh well, I do C++ on a day-to-day basis, and C# for inhouse tools. Besides, I usually don't have any problems with clashing names, so this is really a non-problem for me. -- Weiter, weiter, ins verderben. Wir müssen leben bis wir sterben. I blog too now[^]

                      1 Reply Last reply
                      0
                      • L leppie

                        Dont be so lazy!

                        using System;

                        class B
                        {
                        public static void Main()
                        {
                        if(true)
                        {
                        B b = new B();
                        }
                        { B b = new B(); } // <-- throws no error CS0136
                        }
                        }

                        top secret
                        Download xacc-ide 0.0.3 now!
                        See some screenshots

                        C Offline
                        C Offline
                        Chris Maunder
                        wrote on last edited by
                        #27

                        lol. Reminds me of the line "if you try and make something foolproof, they just make smarter fools" cheers, Chris Maunder

                        1 Reply Last reply
                        0
                        • N Nish Nishant

                          using System;

                          class B
                          {
                          public static void Main()
                          {
                          if(true)
                          {
                          B b = new B();
                          }

                          	B b = new B(); // <-- throws error CS0136
                          }
                          

                          }

                          error CS0136: A local variable named 'b' cannot be declared in this scope because it would give a different meaning to 'b', which is already used in a 'child' scope to denote something else :wtf: :wtf: :wtf:


                          My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com

                          C Offline
                          C Offline
                          Christian Graus
                          wrote on last edited by
                          #28

                          Class B has nothing to do with it, I suspect. It means that you can't create a variable inside the scope that would hide a variable that exists outside it, although in this case the order in which they appear means it does not matter. I agree, this could be handled better, and has spat at me more than once. I could be childish and say that they tried to make it simple for migrating VB programmers, but I suspect that, as was the case with switch statements, they identified a place where C++ can cause errors that are hard to find, and they were overzealous. IMO, this should be a warning. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                          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