![]() |
| | #1 |
| Registered User Join Date: Nov 2001
Posts: 19
| Question about C# scope rules "The scope of a local variable declared in a local-variable-declaration is the block in which the declaration occurs. It is a error to refer to a local variable in a textual position that precedes the variable declarator of the local variable." Java: "The scope of a local variable declaration in a block (§14.4.2) is the rest of the block in which the declaration appears, starting with its own initializer (§14.4) and including any further declarators to the right in the local variable declaration statement. " It seems to me that the only difference is that Java states more explicitly that a local variable can be seen later in the variable declaration statement. I don't see how the C# definition would exclude the variable declaration statement, but that is the only difference I can see. |
| converge is offline | |
| | #2 |
| the hat of redundancy hat Join Date: Aug 2001 Location: Hannover, Germany
Posts: 2,769
| To me both texts seem to say the same. Declare a local variable, which is valid in the block it was declared below or on the same line as the declaration.
__________________ hth -nv She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate." When in doubt, read the FAQ. Then ask a smart question. |
| nvoigt is offline | |
| | #3 |
| Registered User Join Date: Nov 2001
Posts: 19
| Yea, thats what I thought too. However, my professor insists there is a difference, and he wants us to figure it out. |
| converge is offline | |
| | #4 |
| the hat of redundancy hat Join Date: Aug 2001 Location: Hannover, Germany
Posts: 2,769
| Well, being extremly nitpicky, you can see differences. The scope of the C# variable is the whole block. The compiler will flag an error if you refer to the variable before the line of declaration. The scope of the Java variable is from the line of declaration down to the end of the current block. This leads to two different conclusions: a) Two people wrote two documents and while they meant the same, each has it's own way of finding words and building sentences. b) C# creates the variable at block start, the compiler hinders you to access it before the line of declaration while Java creates it at the time it is declared. For the compiler however, these two versions work exactly the same.
__________________ hth -nv She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate." When in doubt, read the FAQ. Then ask a smart question. |
| nvoigt is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Question regarding Memory Leak | clegs | C++ Programming | 29 | 12-07-2007 01:57 AM |
| Design layer question | mdoland | C# Programming | 0 | 10-19-2007 04:22 AM |
| Question... | TechWins | A Brief History of Cprogramming.com | 16 | 07-28-2003 09:47 PM |
| opengl DC question | SAMSAM | Game Programming | 6 | 02-26-2003 09:22 PM |
| Very simple question, problem in my Code. | Vber | C Programming | 7 | 11-16-2002 03:57 PM |