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

    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