Hot File

Create C# Variable Scopes

View: 158    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: C / C++ / MFC   Fields: Other

0 point/0 review File has been tested

The scope of a variable is an area or region of code which indicates that where these variables is being accessed from that area or region.

Introduction

The scope of a variable is an area or region of code which indicates that where these variables is being accessed from that area or region.

There are 3 types of variable’s scope:

  • Class level scope.
  • Method level scope.
  • for, while and do while loop, if and switch statements scope.

Class level scope:

  • When we create a class and declare a variable in that class, this is called class level scope because that variable is being declared in the class scope.
  • Class level scope variables can be accessed in all methods which is declared in the class.
  • The variables which are declared in class or in class level scope are called fields or class member variables.

Example:

class Test
{  // start class scope
 
  // initialize a variable or a class field in class scope
    string colorName = "red";
 
  // declare a method Get() of type void 
    public void GetValue()            
    {
      /* access class field or variable in method, this colorName field is 
         accessible here because it is being initialized in class scope and 
         it is available in this method until a class scope ends */
        Console.WriteLine(colorName); 
    }   // End method definition
 
} // End class scope

Method level scope:

  • When we create a method in a class, declare or initialize a variable inside that method means we are creating a method level scope.
  • Variables are being declared in any method are called local variables.

Example:

class Program
{      // start class scope
       public static void ShowVariable1()
       {  // start first method scope
 
           //initialize an int variable (number) with value 100.
           int number = 100;
 
           // Print number variable.
           Console.WriteLine(number);
 
       }  // End ShowVariable1 ()'s scope
 
       public static void ShowVariable2()     // start second method scope
       {
         // Try to access same local variable (number) which is already declared in above ShowVariable1 () method. compile-error will be occurred.
           Console.WriteLine(number);                    
 
       } // End ShowVariable2 ()'s scope
   }

Remember:

  • You cannot declare local variables twice with the same name in same scope. If you do that you will face a compile time error.

for, while and do while loop, if and switch statements scope:

  • Variables which are being declared inside for, while, if and switch statement’s body are called local variables.
  • These variables define their scope inside the body of these statements, so this is called statements scope.

Example:

class Program
    {  // start class scope
        static void Main(string[] args)         // start main method scope
        {
            for (int i = 0; i <= 10; i++)
            {  // start for loop scope.
 
                // Access that local i variable inside for loop's body.
                Console.WriteLine(i);
            }   // End for loop scope
 
            // Try to access same local variable (i) which has  already declared in above for loop statement. A compile-time error will be occurred.
            Console.WriteLine(i);
            Console.ReadKey();
        }   // End main method scope
    }   // End class scope

Above example is being associated with only for loop, this is not limited, we can use variable’s statements scope with all available statements in C# such as if, switch, do while and while loop statements

Create C# Variable Scopes

Create C# Variable Scopes Posted on 07-09-2016  The scope of a variable is an area or region of code which indicates that where these variables is being accessed from that area or region. 5/10 158

Comment:

To comment you must be logged in members.

Files with category

  • How to Swap Two Numbers using Call by Reference in C

    View: 260    Download: 0   Comment: 0   Author: none  

    How to Swap Two Numbers using Call by Reference in C

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

  • How to Print Fibonacci Series using Recursion in C, C++

    View: 262    Download: 0   Comment: 0   Author: none  

    How to Print Fibonacci Series using Recursion in C, C++

    Category: C / C++ / MFC
    Fields: Other

    4.5/1 review
    Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

  • How to Implicitly Typed Arrays In C#

    View: 291    Download: 0   Comment: 0   Author: none  

    How to Implicitly Typed Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.

  • How to C# Static vs Instance Methods As Event Handlers

    View: 190    Download: 0   Comment: 0   Author: none  

    How to C# Static vs Instance Methods As Event Handlers

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    Both static and instance methods can be used as event handlers in C#

  • Build Pass By Reference To Method In C#

    View: 339    Download: 0   Comment: 0   Author: none  

    Build Pass By Reference To Method In C#

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    Like value types such as int, double, char etc. We can also pass a reference variable to the method in C# as its parameter which allows us to pass an object to the method and it is called pass by reference to method in C#.

  • How to C# Continue Statement

    View: 178    Download: 0   Comment: 0   Author: none  

    How to C# Continue Statement

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    continue statement skips or exits the current execution of a loop by given condition and keep execution continue for its next iteration unlike break statement it does not exit or terminate from loops, it terminates only its current execution

  • How to Single Dimensional Arrays In C#

    View: 131    Download: 0   Comment: 0   Author: none  

    How to Single Dimensional Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/1 review
    Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an...

  • Build C# Multilevel Inheritance

    View: 145    Download: 0   Comment: 0   Author: none  

    Build C# Multilevel Inheritance

    Category: C / C++ / MFC
    Fields: Other

    2.5/2 review
    C# supports multilevel inheritance, suppose we have four classes A, B, C and D. Class A can inherit to B, B to C and C can inherit to D and so on now class D has all public members of class A, B and C include its own.

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd