By 22/07/2015

Local Variables In C#

  • Local variables in C# are declared inside methods, switch, loops, if or any other statement type scopes.
  • Local variables have limited life time because these alive in only scope where they were created, they remain alive or accessible until that scope ends. They cannot be accessed outside of its scope.
  • In object oriented programming pattern these local variables are passed to methods as a parameters.

Example:

using System;
namespace csharpBasic
{
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            /* Following is for loop declaration,
               NOTICE: an int variable localVar (it is a local variable) 
               is being initialized with value 1.
             */
            for (int localVar = 1; localVar <= 5; localVar++)
            { // for loop scope starts

                // A local variable is being printed.
                Console.WriteLine(localVar);

            } /*for loop scope Ends.
                NOTE: you cannot access localVar variable outside of this scope,
                because it was only accessible inside its scope. 
               */

            // Uncomment following code will generate compile-time error.
            // Console.WriteLine(localVar);
            Console.ReadKey();
        }
        /*
        The Output will be:
        1
        2
        3
        4
        5
        */ 
    }
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Comments are closed.

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .