
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Scope of Variables in C#
The scope of a variable is a region of code that indicates where the variables are being accessed.
For a variable, it has the following levels −
Method Level
Variable declared inside a method is a local variable.
Class Level
Variable declared inside a class is a local variable are class member variables.
Let us see an example of scope of variables −
Example
using System; namespace Demo { class Program { public int Divide(int num1, int num2) { // local variable in a method int result; result = num1 / num2; return result; } static void Main(string[] args) { // local variable int a = 150; int b = 10; int res; Program p = new Program(); res = p.Divide(a, b); Console.WriteLine("Division Result = {0}", res ); Console.ReadLine(); } } }
Output
Division Result = 15
- Related Articles
- Scope of Variables in Python
- Scope of Variables in C++
- C++ Scope of Variables
- Global Scope Variables in Postman?
- What is the scope of variables in JavaScript
- Scope and lifetime of variables in Java?\n
- What is the scope of local variables in Java?
- Variables, their types, and Scope in C++
- What are Local Scope Variables in Postman?
- Access variables from parent scope in anonymous PHP function
- What do you mean by Scope of variables inside MySQL stored procedure?
- Scope of RPA
- Explain the Scope and Scope Chain in JavaScript
- Avoid Scope Creep: Control Your Project Scope
- Scope and Excitement of Physics

Advertisements