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. c# no concept of scope ?

c# no concept of scope ?

Scheduled Pinned Locked Moved C#
questioncsharp
11 Posts 5 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.
  • J jpribele

    what scewey logic is c# using for scope? why does the second declaration of lenum cause this code not to compile ? but j is ok ?

    using System;
    using System.Collections;
    
    public class Test  {
    
    	public static int Main(String[] args) {
    
    	ArrayList b = new ArrayList();
    	ArrayList c = new ArrayList();
    	for( int i = 0; i < b.Count; i++ ) {
    		if( true ) {
    			IEnumerator lenum = b.GetEnumerator();
    			while( lenum.MoveNext() ) {
    				int j = 0;
    				j++;
    			}
    		}
    		IEnumerator lenum = c.GetEnumerator();
    		while( lenum.MoveNext() ) {
    			int j = 0;
    			j++;
    		}
    		
    	}
    	return 0;
    	}
    
    }
    
    R Offline
    R Offline
    Roger Stewart
    wrote on last edited by
    #2

    My guess is that the C# compile knows that the if( true ) block will ALWAYS be executed and optimizes the if statement out. The result is that lenum = b.GetEnumerator() and lenum = c.GetEnumerator() are in the same scope. Roger Stewart "I Owe, I Owe, it's off to work I go..."

    J 1 Reply Last reply
    0
    • R Roger Stewart

      My guess is that the C# compile knows that the if( true ) block will ALWAYS be executed and optimizes the if statement out. The result is that lenum = b.GetEnumerator() and lenum = c.GetEnumerator() are in the same scope. Roger Stewart "I Owe, I Owe, it's off to work I go..."

      J Offline
      J Offline
      jpribele
      wrote on last edited by
      #3

      i just did that as a short example. my actual code where i found that oddity does the same thing.

      1 Reply Last reply
      0
      • J jpribele

        what scewey logic is c# using for scope? why does the second declaration of lenum cause this code not to compile ? but j is ok ?

        using System;
        using System.Collections;
        
        public class Test  {
        
        	public static int Main(String[] args) {
        
        	ArrayList b = new ArrayList();
        	ArrayList c = new ArrayList();
        	for( int i = 0; i < b.Count; i++ ) {
        		if( true ) {
        			IEnumerator lenum = b.GetEnumerator();
        			while( lenum.MoveNext() ) {
        				int j = 0;
        				j++;
        			}
        		}
        		IEnumerator lenum = c.GetEnumerator();
        		while( lenum.MoveNext() ) {
        			int j = 0;
        			j++;
        		}
        		
        	}
        	return 0;
        	}
        
        }
        
        L Offline
        L Offline
        leppie
        wrote on last edited by
        #4

        This is no different to C. Just brace it.

        public class Test
        {
        public static int Main(String[] args)
        {
        ArrayList b = new ArrayList();
        ArrayList c = new ArrayList();
        if( true )
        {
        IEnumerator lenum = b.GetEnumerator();
        while( lenum.MoveNext() )
        {
        int j = 0;
        j++;
        }
        }
        { //see no problem now
        IEnumerator lenum = c.GetEnumerator();
        while( lenum.MoveNext() )
        {
        int j = 0;
        j++;
        }
        }
        return 0;
        }
        }

        leppie::AllocCPArticle("Zee blog");

        1 Reply Last reply
        0
        • J jpribele

          what scewey logic is c# using for scope? why does the second declaration of lenum cause this code not to compile ? but j is ok ?

          using System;
          using System.Collections;
          
          public class Test  {
          
          	public static int Main(String[] args) {
          
          	ArrayList b = new ArrayList();
          	ArrayList c = new ArrayList();
          	for( int i = 0; i < b.Count; i++ ) {
          		if( true ) {
          			IEnumerator lenum = b.GetEnumerator();
          			while( lenum.MoveNext() ) {
          				int j = 0;
          				j++;
          			}
          		}
          		IEnumerator lenum = c.GetEnumerator();
          		while( lenum.MoveNext() ) {
          			int j = 0;
          			j++;
          		}
          		
          	}
          	return 0;
          	}
          
          }
          
          S Offline
          S Offline
          scadaguy
          wrote on last edited by
          #5

          The scopes of the two lenum variables overlap whereas those of the j variables do not. Read section 5.1.7 of the C# language specification. Within the scope of a local variable, it is a compile-time error to refer to the local variable in a textual position that precedes its variable-declarator. However... The lifetime of a local variable is the portion of program execution during which storage is guaranteed to be reserved for it. This lifetime extends from entry into the block, for-statement, switch-statement, or using-statement with which it is associated, until execution of that block, for-statement, switch-statement, or using-statement ends in any way. These two statements together seem to indicate that the compiler is doing what it is suppose to. I wonder why the scope of a variable starts at the entry point of the block and not at the declaration point? Afterall, if you can't reference the variable in a textual position preceding the declaration then why not define its scope in the same manner? I'm sure there's a good answer. I just don't know what it is :) Brian Brian

          J 1 Reply Last reply
          0
          • S scadaguy

            The scopes of the two lenum variables overlap whereas those of the j variables do not. Read section 5.1.7 of the C# language specification. Within the scope of a local variable, it is a compile-time error to refer to the local variable in a textual position that precedes its variable-declarator. However... The lifetime of a local variable is the portion of program execution during which storage is guaranteed to be reserved for it. This lifetime extends from entry into the block, for-statement, switch-statement, or using-statement with which it is associated, until execution of that block, for-statement, switch-statement, or using-statement ends in any way. These two statements together seem to indicate that the compiler is doing what it is suppose to. I wonder why the scope of a variable starts at the entry point of the block and not at the declaration point? Afterall, if you can't reference the variable in a textual position preceding the declaration then why not define its scope in the same manner? I'm sure there's a good answer. I just don't know what it is :) Brian Brian

            J Offline
            J Offline
            jpribele
            wrote on last edited by
            #6

            so its not a bug because they say its feature. nice. any other decent language would let this code compile. oh I'll just add it to my list of screwed up things c# does

            L 1 Reply Last reply
            0
            • J jpribele

              so its not a bug because they say its feature. nice. any other decent language would let this code compile. oh I'll just add it to my list of screwed up things c# does

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #7

              So C is not decent. Get a life or go back to whatever decent language you were using previously. PS: did you even read my reply? PSS: using a foreach loop allows you to do that. PSSS: You are infact constructing your IEnumerator loop incorrectly. Here is the proper way:

              IEnumerable nodes;
              ....

              for (IEnumerator enumer = nodes.GetEnumerator(); enumer.MoveNext(); )
              {
              object node = enumer.Current;
              ...
              }

              leppie::AllocCPArticle("Zee blog");

              J S 2 Replies Last reply
              0
              • J jpribele

                what scewey logic is c# using for scope? why does the second declaration of lenum cause this code not to compile ? but j is ok ?

                using System;
                using System.Collections;
                
                public class Test  {
                
                	public static int Main(String[] args) {
                
                	ArrayList b = new ArrayList();
                	ArrayList c = new ArrayList();
                	for( int i = 0; i < b.Count; i++ ) {
                		if( true ) {
                			IEnumerator lenum = b.GetEnumerator();
                			while( lenum.MoveNext() ) {
                				int j = 0;
                				j++;
                			}
                		}
                		IEnumerator lenum = c.GetEnumerator();
                		while( lenum.MoveNext() ) {
                			int j = 0;
                			j++;
                		}
                		
                	}
                	return 0;
                	}
                
                }
                
                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                You could try using a compiler that doesn't punish good programmers for using perfectly legitimate code. Technically, there is no conflict so long as the outer scope lenum comes after the inner scope one. MS's csc and Mono's mcs both use this buggy scoping, however DotGNU Portable.NET's cscc allows this code (as well it should) since there is no conflict. It does reject code where the outer scope variable is declared before the inner scope variable, however (since in that case there is an actual conflict). Rich P.S. For more info on DotGNU check out: http://www.dotgnu.org/

                1 Reply Last reply
                0
                • L leppie

                  So C is not decent. Get a life or go back to whatever decent language you were using previously. PS: did you even read my reply? PSS: using a foreach loop allows you to do that. PSSS: You are infact constructing your IEnumerator loop incorrectly. Here is the proper way:

                  IEnumerable nodes;
                  ....

                  for (IEnumerator enumer = nodes.GetEnumerator(); enumer.MoveNext(); )
                  {
                  object node = enumer.Current;
                  ...
                  }

                  leppie::AllocCPArticle("Zee blog");

                  J Offline
                  J Offline
                  jpribele
                  wrote on last edited by
                  #9

                  leppie wrote: So C is not decent. Get a life or go back to whatever decent language you were using previously. you would think a made i made a comment about your mother. PS: did you even read my reply? i did read your reply but you didn't explain the problem you just posted a work around. PSSS: You are infact constructing your IEnumerator loop incorrectly. Here is the proper way: incorrectly ? there is very little difference between the two and in my opinion the while loop is more readably, you don't have an empty statement as in your for loop. what's the deal anyways. i make a couple of comments about the quirks in the language( every language has them ) and you go off on me like i declare C# as the worst language in the world and every programmer who uses is stupid. now if i have offended anyone, i apologize. but you should remember its just another programming language. i don't think knights running around defending its honour are necessary.

                  L 1 Reply Last reply
                  0
                  • L leppie

                    So C is not decent. Get a life or go back to whatever decent language you were using previously. PS: did you even read my reply? PSS: using a foreach loop allows you to do that. PSSS: You are infact constructing your IEnumerator loop incorrectly. Here is the proper way:

                    IEnumerable nodes;
                    ....

                    for (IEnumerator enumer = nodes.GetEnumerator(); enumer.MoveNext(); )
                    {
                    object node = enumer.Current;
                    ...
                    }

                    leppie::AllocCPArticle("Zee blog");

                    S Offline
                    S Offline
                    scadaguy
                    wrote on last edited by
                    #10

                    I think you may have misunderstood what the point of the original post was. It was a question about how C# handles scoping. It is a very legitimate and intelligent question. The author was not looking for a work-around. Brian

                    1 Reply Last reply
                    0
                    • J jpribele

                      leppie wrote: So C is not decent. Get a life or go back to whatever decent language you were using previously. you would think a made i made a comment about your mother. PS: did you even read my reply? i did read your reply but you didn't explain the problem you just posted a work around. PSSS: You are infact constructing your IEnumerator loop incorrectly. Here is the proper way: incorrectly ? there is very little difference between the two and in my opinion the while loop is more readably, you don't have an empty statement as in your for loop. what's the deal anyways. i make a couple of comments about the quirks in the language( every language has them ) and you go off on me like i declare C# as the worst language in the world and every programmer who uses is stupid. now if i have offended anyone, i apologize. but you should remember its just another programming language. i don't think knights running around defending its honour are necessary.

                      L Offline
                      L Offline
                      leppie
                      wrote on last edited by
                      #11

                      jpribele wrote: now if i have offended anyone, i apologize. but you should remember its just another programming language. i don't think knights running around defending its honour are necessary. I think I have misunderstood too. :doh: (rough week/boring weekend) I apologize. From the post below by Rich333 (pnet developer) he clarified it. :) It appears to be a EMCA spec interpretation problem. leppie::AllocCPArticle("Zee blog");
                      Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

                      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