• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C# » Introduction » What is Scope of Variable
Next →
← Prev

What is Scope of Variable

By Dinesh Thakur

In the body of a method, you can also declare variables that would be used internally. A variable declared in the body is referred to as a local variable. It cannot be accessed outside of the method it belongs to. After declaring a local variable, it is made available to the method and you can use it.

As mentioned with functions, C# doesn’t have the notion of global variables (in C/C++,

Visual Basic, Pascal, etc, a global variable is one that is declared outside of any class; such a variable is made available to any function, or even file, of the same program without being declared again where needed). Still, if you want to use the same variable in various methods of the main class, you can declare it outside of any method. The variable must be declared as static. That is, you must type the static keyword to its left when declaring it. Here is an example:

 

Using System;

Class Exercise

{

Static double Length;

Static void welcome()

{

console. WriteLine (“welcome to the wonderful world of c#”);

}

Static void Main ()

{

Welcome();

console. WriteLine ();

}

}

After declaring such a variable, you can access from any method that belongs to the same class. Here is an example, to understand the concept of static variable.

Use of static variable

 

using System;

class Cylinder

{

static double Length;

static double Width;

static double Area;

static double Get The Length ()

{

double Len;

console. Write (“Length: “);

Len= double. Parse (console. Read Line ());

return 1en;

}

static double GetThewidth()

{

 

double w;

console. Write \(“width: “);

w = double. Parse (console. Read Line ());

return w;

}

static void AreaRect ()

{

console. WriteLine (“Enter the Length and width of Rectangle “);

width = Get The width();

Length = Get The Length();

Area = Length * width;

}

static void show Area()

{

console. Write Line(“\n characteristics of Rectangle”);

console. write Line(“Length: “+ Length);

console. Write Line(“width :”+ width);

Console. Write Line(“Area :”+ Area); }

}

static void Main()

{

AreaRect ();

Show Area() ;

console. Write Line (); }

}

 }

 

OUTPUT:

 

Enter the Length and Width of Rectangle

Length: 38.24

Width: 32.58

Characteristics of Rectangle

Length: 38.24

Width: 32.58

Area : 1245.85

You’ll also like:

  1. Variable’s Scope and Lifetime of a Variable
  2. Scope of a Variable in Java
  3. Define the term Scope, Visibility And Lifetime of a Variable
  4. Difference Between Declaring a Variable and Defining a Variable
  5. Final Variable (OR Constant Variable) in Java with Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C# Tutorials

C# Tutorials

  • C# - .NET Languages Types
  • C# - Dot Net
  • C# - Dot Net Framework
  • C# - Inheritance Types
  • C# - Features
  • C# - CTS
  • C# - CLS
  • C# - CLR
  • C# - Console
  • C# - MSIL
  • C# - Base Class Library
  • C# - Web Forms Creation
  • C# - C# Vs C++
  • C# - Statements Types
  • C# - JIT
  • C# - CLI
  • C# - Controls Types
  • C# - String Types
  • C# - Execution Model

Other Links

  • C# - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW