C# – Variables

A Variable is the name given to a memory location holding a particular type of data. so, each variable has associated with it a data type and a value type.

Integral typessbyte, byte, short, ushort, int, uint, long, ulong, and char
Floating point typesfloat and double
Decimal typesdecimal
Boolean typestrue or false values, as assigned
Nullable typesNullable data types

C# also allows defining other value types of variable such as Enum and reference types of variable such as class , which we will cover in subsequent chapters.

Defining Variables

Syntax for variable definition in c# is –

<data_type> <variable_list>;

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.

Examples :-

int I,j,k;

char c, ch;

float f, salary;

double d;

You can initialize a variable at the time of definition as –

int i = 100;

Initializing Variables

Variables are initialized (assigned a value) with an equal sign followed by a constant expression. The general form of initialization is –

Variable_name = value;

Variables can be initialized in their declaration. The initializer consists of an equal sign followed by a constant expression as –

<data_type> <variable_name> = value;

Leave a Reply

Your email address will not be published. Required fields are marked *