Variable Declaration
Variables are elements created during the execution of the program and used to store data.
When creating variables, their names and the type of data they will store are determined.
This name will be used when the variable is to be given a value, or when it is desired to reach the value stored by the variable.
As soon as a variable is created, a place in computer memory is reserved for that variable. The size of the allocated space depends on the data type of the variable.
While defining (creating) a variable, we should think carefully about what type of data it will store and decide accordingly. For example, if we define a variable to store numbers, we cannot then assign text to that variable.
Data Types
We can divide numbers into two groups, integers and decimals.
There are 4 different data types that we can use to store integers. ( byte, short, int, long )
Three different data types can be used to store decimals. ( float, double, decimal )
We can use the “Char” data type to store a single character text, and the “String” data type to store longer texts.
The table below shows which data type will store which information and how much space will be allocated in the memory.
Click on image to enlarge.
Variable Declaration
The variable definition process is done as follows: First, the data type of the variable is written, then the name to be given to the variable is written by leaving a space.
Sample:
int a;
string answer;
float average;
When defining a variable, its value can be given at that time if desired:
int a=12;
float average=7.5f;
decimal number2=2155.32m;
string address= “New neighborhood Thought Street”;
Multiple variables of the same type can be defined in one line:
int a, b, i;
double x, y;
Again, when defining more than one variable in a row, their values can also be given:
int a=5, b=23, c=56;
double x=2.34, y=3.02;
string name=”Selin”, last name=”Aydoğdu”;
WARNING : When assigning values to float and decimal type variables, a suffix is used after the given value. For float f or F, m or M type is used for decimal. And there is no space before these suffixes.
float count=0.45f;
Decimal mean=1.4m;
Also, dot should be used when separating the decimal part in decimal numbers.
Variable Naming Conventions
When naming variables, different characters ( ı, İ, ğ, Ğ, ü, Ü, ş, Ş, ö , Ö, ç, Ç ), spaces and special characters ( . , ; : / etc.) should not be used.
In addition, words that have another meaning in the programming language should not be chosen as variable names. (int, not, if, char etc.)
Variable names cannot start with digits or numbers. A variable can be named quiz1 but not 1quiz.
defining variable in c#, variable definition, variable instances, differences between variable types, what does a variable do, difference between float and double
EXERCISES
There are no examples related to this subject.
Read 763 times.