Variables
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 )
One character to store a text "Char" data type, even long texts is to store "String" We can use the data type.
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 Definition
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. Use f or F for float type and m or M for decimal type . 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, some 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 not1 but not 1not.
What are variables in c#, what is a variable, how to use variable definition, variable types and their differences, data types and differences, variable naming conventions
EXERCISES
There are no examples related to this subject.
Read 830 times.