Variables in C#

Before talking about variable we shall first know What is a variable ?
A variable is a location in a memory that has a name and holds some value. the value can be an integer, character, decimal. A variable is declared with a specific data type in order to represent what value the variable holds.

Naming the variables in C#

1. Must begin with a character or an underscore and it should be followed by sequence of characters. can not declare a variable which starts with a digit or any other special symbols (other than underscore).

2. a variable should not contain any special symbols . keywords can not be used as variable names.

Examples for valid variable names
Game_level
Myclass (using pascal case).
sum_of_individual_num.

invalid variable names
#result
2score

Note: C# is a case sensitive language. It means that uppercase letters are different from lower case. exam: TennisPlayerName is diff from tennisplayername

Declaring a variable in C#
datatype variablename ;

we can also define a variable at the time of declaration
datatype variablename=value;

exam:
int result;
char choice='v';

data types in C#

predefined type Bytes Range

1. char 2bytes range (0 to 65535).
2. int 4bytes range (-2,147,483,648 to 2,147,483,647)
3. float 4bytes
4. double 8bytes
5. bool 1byte specifies whether true or false true or false


they r mainly two type of data types used
1. value type.
The value type includes int, float, char. these directly contain data.
2. reference type.

These do not contain any data but contain reference to variables which are stored in memory. example of reference type is 'string' .

0 Comments:

Post a Comment



Newer Post Older Post Home