Part 10 C# Type of Variables and Their Scope

Channel:
Subscribers:
8,310
Published on ● Video Link: https://www.youtube.com/watch?v=tC8wTTFiQXI



Duration: 11:04
619 views
11


Types of Variables
& Scope of Variables
Local variables, Instance Variables, Global Variables, Fields,
Class Variables, Constant Variables
A variable provides us with named storage that our programs can manipulate. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C++ is case-sensitive −
A variable definition tells the compiler where and how much storage to create for the variable. A variable definition specifies a data type, and contains a list of one or more variables of that type as follows –
Here, type must be a valid C++ data type including char, w_char, int, float, double, bool or any user-defined object, etc., and variable_list may consist of one or more identifier names separated by commas. Some valid declarations are shown here –
A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a specific type, which determines the size and layout of the variable's memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable.
Here, data_type must be a valid C# data type including char, int, float, double, or any user-defined data type, and variable_list may consist of one or more identifier names separated by commas.
C# also allows defining other value types of variable such as enum and reference types of variables such as class, which we will cover in subsequent chapters.
Variables are initialized (assigned a value) with an equal sign followed by a constant expression.
Variables can be initialized in their declaration.
It is a good programming practice to initialize variables properly, otherwise sometimes program may produce unexpected result.
The Console class in the System namespace provides a function ReadLine()for accepting input from the user and store it into a variable.
Variables are lvalues and hence they may appear on the left-hand side of an assignment. Numeric literals are rvalues and hence they may not be assigned and can not appear on the left-hand side.
• Local variables are declared in methods, constructors, or blocks.
• Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.
• Access modifiers cannot be used for local variables.
• Local variables are visible only within the declared method, constructor, or block.
• Local variables are implemented at stack level internally.
• There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use.
• Instance variables are declared in a class, but outside a method, constructor or any block.
• When a space is allocated for an object in the heap, a slot for each instance variable value is created.
• Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.
• Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class.
• Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
• There would only be one copy of each class variable per class, regardless of how many objects are created from it.
• Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final, and static. Constant variables never change from their initial value.







Tags:
types of variables
variable
types
programming
variable types
tutorial
tutorials
Local variables
Instance Variables
Global Variables
Fields
Class Variables
Constant Variables
Scope of Variables
scope
scope of variables in C#
local variables
variable scope
create object
object reference
C#
var scope
var types
how many types of var
how many types of variables
what is var scope
why var not accessing in same function
whats is fields
urdu
C# hindi tuto