Thread: Question about C# scope rules

  1. #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.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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.

  3. #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.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  2. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM