C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-28-2002, 11:08 PM   #1
Registered User
 
Join Date: Nov 2001
Posts: 19
Question about C# scope rules

I was wondering what the fundamental difference is between C# scope rules of local variables and Java's. I know there is a difference, but I can't figure it out. Here is the scope rule from the C# language definition:
"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   Reply With Quote
Old 01-29-2002, 01:21 AM   #2
the hat of redundancy hat
 
nvoigt's Avatar
 
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   Reply With Quote
Old 01-29-2002, 09:25 AM   #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   Reply With Quote
Old 01-30-2002, 06:56 AM   #4
the hat of redundancy hat
 
nvoigt's Avatar
 
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:48 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22