C# Variables Variables in c# How to use variables in c# Types of Variables #5



C# Variables
Variables are containers for storing data values.
In C#, there are different types of variables (defined with different keywords).

#CSharp #variables #tutorial # beginners #Examples
int – stores integers (whole numbers), without decimals, such as 123 or -123
double – stores floating point numbers, with decimals, such as 19.99 or -19.99
char – stores single characters, such as ‘a’ or ‘B’. Char values are surrounded by single quotes
string – stores text, such as “Hello World”. String values are surrounded by double quotes
bool – stores values with two states: true or false

Declaring (Creating) Variables

To create a variable, you must specify the type and assign it a value:
Syntax

type variableName = value;

Wh

ere type is a C# type (such as int or string), and variableName is the name of the variable (such as x or name). The equal sign is used to assign values to the variable.

To create a variable that should store text, look at the following example:
Example

Create a variable called name of type string and assign it the value “John”:

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.

A variable is a name given to a memory location and all the operations done on the variable effects that memory location. In C#, all the variables must be declared before they can be used. It is the basic unit of storage in a program. The value stored in a variable can be changed during program execution.
Types of Variables

Local variables
Instance variables or Non – Static Variables
Static Variables or Class Variables
Constant Variables
Readonly Variables

Local Variables

A variable defined within a block or method or constructor is called local variable.

These variables are created when the block is entered or the function is called and destroyed after exiting from the block or when the call returns from the function.
The scope of these variables exists only within the block in which the variable is declared. i.e. we can access these variables only within that block.
C# Variable

A variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times.

It is a way to represent memory location through symbol so that it can be easily identified.

Comments are closed.