Subscribe

RSS Feed (xml)

The Scope and Lifetime of Variables in C sharp

So far, all of the variables that we have been using were declared at the start of the Main( ) method. However, C# allows a local variable to be declared within any block. A block is begun with an opening curly brace and ended by a closing curly brace. A block defines a declaration space, or scope. Thus, each time you start a new block, you are creating a new scope. A scope determines what objects are visible to other parts of your program. It also determines the lifetime of those objects.

The most important scopes in C# are those defined by a class and those defined by a method. A discussion of class scope (and variables declared within it) is deferred until later in this book, when classes are described. For now, we will examine only the scopes defined by or within a method.

The scope defined by a method begins with its opening curly brace. However, if that method has parameters, they too are included within the method’s scope.

As a general rule, variables declared inside a scope are not visible (that is, accessible) to code that is defined outside that scope. Thus, when you declare a variable within a scope, you are localizing that variable and protecting it from unauthorized access and/or modification. Indeed, the scope rules provide the foundation for encapsulation.

Scopes can be nested. For example, each time you create a block of code, you are creating a new, nested scope. When this occurs, the outer scope encloses the inner scope. This means that objects declared in the outer scope will be visible to code within the inner scope. However, the reverse is not true. Objects declared within the inner scope will not be visible outside it.

To understand the effect of nested scopes, consider the following program:

// Demonstrate block scope.

using System;

class ScopeDemo {
 public static void Main() {
   int x; // known to all code within Main()

   x = 10;
   if(x == 10) { // start new scope
     int y = 20; // known only to this block

     // x and y both known here.
     Console.WriteLine("x and y: " + x + " " + y);
     x = y * 2;
   }
   // y = 100; // Error! y not known here

   // x is still known here.
   Console.WriteLine("x is " + x);
 }
}

As the comments indicate, the variable x is declared at the start of Main( )’s scope and is accessible to all subsequent code within Main( ). Within the if block, y is declared. Since a block defines a scope, y is visible only to other code within its block. This is why outside of its block, the line y = 100; is commented out. If you remove the leading comment symbol, a compile-time error will occur, because y is not visible outside of its block. Within the if block, x can be used because code within a block (that is, a nested scope) has access to variables declared by an enclosing scope.

Within a block, variables can be declared at any point, but are valid only after they are declared. Thus, if you define a variable at the start of a method, it is available to all of the code within that method. Conversely, if you declare a variable at the end of a block, it is effectively useless, because no code will have access to it.

Here is another important point to remember: Variables are created when their scope is entered and destroyed when their scope is left. This means that a variable will not hold its value once it has gone out of scope. Therefore, variables declared within a method will not hold their values between calls to that method. Also, a variable declared within a block will lose its value when the block is left. Thus, the lifetime of a variable is confined to its scope.

If a variable declaration includes an initializer, then that variable will be reinitialized each time the block in which it is declared is entered. For example, consider this program:

// Demonstrate lifetime of a variable.

using System;

class VarInitDemo {
 public static void Main() {
   int x;

   for(x = 0; x < 3; x++) {
     int y = -1; // y is initialized each time block is entered
     Console.WriteLine("y is: " + y); // this always prints -1
     y = 100;
     Console.WriteLine("y is now: " + y);
   }
 }
}

The output generated by this program is shown here:

y is: -1
y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100

As you can see, y is always reinitialized to 1 each time the body of the for loop is entered. Even though it is subsequently assigned the value 100, this value is lost.

There is one quirk to C#’s scope rules that may surprise you: Although blocks can be nested, no variable declared within an inner scope can have the same name as a variable declared by an enclosing scope. For example, the following program, which tries to declare two separate variables with the same name, will not compile:

/*
  This program attempts to declare a variable
  in an inner scope with the same name as one
  defined in an outer scope.

  *** This program will not compile. ***
*/

using System;

class NestVar {
 public static void Main() {
   int count;

   for(count = 0; count < 10; count = count+1) {
     Console.WriteLine("This is count: " + count);

     int count; // illegal!!!
     for(count = 0; count < 2; count++)
       Console.WriteLine("This program is in error!");
   }
 }
}

If you come from a C/C++ background, then you know that there is no restriction on the names that you give variables declared in an inner scope. Thus, in C/C++ the declaration of count within the block of the outer for loop is completely valid. However, in C/C++, such a declaration hides the outer variable. The designers of C# felt that this name hiding could easily lead to programming errors and disallowed it.

15 comments:

  1. Please do more articles like this in the future. Very informational and knowledgeable. I will expect more from you in the future. For now i will just bookmark your page and surely I'm gonna come back later to read more. Thank you to the writer!


    www.imarksweb.org

    ReplyDelete
  2. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.
    dot net training in chennai velachery | top10 dot net training institutes in chennai

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Great post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge.student review for Realtime Experts marathahalli bangalore

    ReplyDelete
  5. An intriguing discussion is worth comment.
    I think that you ought to publish more on this subject matter,
    it might not be a taboo matter but generally people don't discuss these issues. To the next! Kind regards!!
    Click here to Read more.

    ReplyDelete
  6. Pertanian adalah seni dan ilmu menumbuhkan tanaman dan tanaman lain serta memelihara hewan untuk makanan, kebutuhan manusia lainnya, atau keuntungan ekonomi. cek juga yukusaha dan Mengintip Cara Budidaya Jamur Tiram Dengan Mudah Bagi Pemula

    ReplyDelete
  7. Interesting to read and useful article with detailed explanation. Thanks for sharing Angular training in Chennai

    ReplyDelete
  8. It is a more helpful and time saving post. What is a Turkish visa online ? It is nothing more than a substitute for a Turkish physical visa. but the application process is more simple than the Turkish physical visa. You can also check the details about the Turkish online visa.

    ReplyDelete

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail