Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I think of the scope of a variable as -

"The scope of a particular variable is the range within a program's source code in which that variable is recognized by the compiler".

That statement is from "Scope and Lifetime of Variables in C++", which I read many months ago.

Recently I came across this in LeMoyne-Owen College courses:

http://sankofa.loc.edu/CHU/WEB/Courses/Cosi350/Ch4/scope.13.gif

What exactly is the difference between the scope of variables in C# and (C99, C++, Java) when

However a variable still must be declared before it can be used

share|improve this question
1  
I think the first part of the slide is wrong. There are places where a statement can appear, but variable declarations can't. For example, if (true) int x; won't compile, but if (true) M(); will. –  svick Mar 28 '12 at 22:54

3 Answers 3

In C++ and Java the following code is legal, but in C# it would cause this conflict: "local variable 'logging' cannot be declared in this scope because it would give a different meaning to 'logging', which is already used in a child scope".

// Statically check script.
{
    Boolean logging = false;
    MyClass.check(script, logging);
}

// Translate code.
Boolean logging = true;
MyClass.translate(script, logging);
share|improve this answer

What is meant by the "scope" of a variable? think of the scope of a variable as "The scope of a particular variable is the range within a program's source code in which that variable is recognized by the compiler".

That definition matches the definition of scope in the C# specification; it says:

The scope of a name is the region of program text within which it is possible to refer to the entity declared by the name without qualification of the name

Notice that the C# specification calls out that it is the name which has a scope. Some variables do not have names, and therefore have no scope. And some things have names but are not variables; class names, namespace names, function names, and so on, all have a scope.

The scope of a local variable is frequently confused with its lifetime. The lifetime of a local variable is sometimes related to its scope; when control leaves the scope of a local (either through normal means or via an exception) then the local can be destroyed without anyone noticing. However, the runtime is permitted to make the lifetime of a local variable shorter if it can do so without anyone noticing. The runtime is also permitted to make a local variable live longer than its scope. And there are some situations in which a local variable is required to live beyond when control leaves its scope. (For instance, when the local is a closed-over outer variable of a lambda.)

Therefore it is unwise to use "scope" as a synonym for "lifetime". They are often related but can be quite different. Use "scope" to mean "the region of text in which a name is valid", not "the period of time in which a variable is alive". Scope is fundamentally a compile-time concept.

What exactly is the difference between the scope of variables in C# and (C99, C++, Java)

I am not sufficiently familiar with the exact scoping rules of C, C++ or Java to give a short list of the differences. I can however point out some oddities of C#.

The first is that in C#, a local variable is in scope throughout its block.

class C
{
    int x;
    void M()
    {
        int y = x;
        int x = y;
    }
}

In C++, the first "x" means "this.x" because the local variable x is not in scope until its declaration. In C#, the local variable x is in scope throughout the block. Using a local that is in scope before its declaration is an error in C#.

The second is that in C#, a simple name must mean the same thing throughout the scope in which it is first used:

class C
{
    int x;
    void M()
    { // M starts
        int y = x;
        if (y > 100)
        {
            int x = y;
        }
    }
}

This is illegal because inside the block labelled "M starts" the simple name "x" is used to mean two different things. Now the first "x" means "this.x" because the local is not in scope. But it is illegal to use the same name to mean two different things, because that is a source of bugs.

If this subject interests you, I have written a number of articles on it. See

http://blogs.msdn.com/b/ericlippert/archive/tags/scope/

http://blogs.msdn.com/b/ericlippert/archive/tags/declaration+spaces/

share|improve this answer

The term "scope", here, refer to the time that a variable exist in life. It starts when the constructor is called and ends when the destructor is called.

In C++ and Java the variable begin to exist when its definition is encountered, and end it existence when the end of block that contains such definition is reached (either by the sequential flow or by a "jump-ahead" instruction like break, goto, return, throw)

In C# all variables declared in a block are created at block entrance, but initialized accordingly to their definition (so they cannot be used before, although they already "exist" in term of allocated space, and hence can be referenced, since their address is already known)

Inside a local block this is much more a sophism, that doesn't make any real difference to the programmer, but -on wider scopes- lets the compiler able to generate the code also in presence of "circular dependencies", that in C++ have to be avoided with other tricks like header guards or two-phase construction etc.

share|improve this answer
4  
you seem to be confusing scope and lifetime here blogs.msdn.com/b/ericlippert/archive/2009/08/03/… though your conclusion is sound –  jk. Mar 23 '12 at 12:28
2  
@jk: It's not me. I'm just referring o the OP terminology that is in turn citing something else ... After all, despite the C++ or C# specs can say, the word "scope" is also a plain English term, that can refer to anything, including both visibility and lifetime. –  Emilio Garavaglia Mar 23 '12 at 14:02
    
@EmilioGaravaglia Can you give a small piece of code as an example ? :) Thanks :) –  jsp99 Mar 24 '12 at 4:32

protected by gnat Aug 3 at 7:41

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.