VARIABLE TYPE CONVERSION
When a variable is defined, the type of memory allocated to it is also determined. Therefore, assigning a different type of value to that variable will cause an error.
For example, we asked the user to enter a number and we want to get the entered number into an int variable:
Console.Write("Enter Number: ");
int a = Console.ReadLine();
An error will occur when using as above. Because everything written to the console screen is considered as a string. We cannot directly assign these values to a variable of numeric type. In order to perform this assignment, we need to use one of the conversion methods mentioned below.
Note: The Write and WriteLine methods of the Console class convert all types to strings and write them to the screen. With these methods, we do not need to make any additional conversions while printing numeric variables to the screen.
If we are going to operate between variables of different types, we need to perform variable conversion.
We can divide variable transformation operations into two: explicit and implicit transformation.
1- Closed Conversion:
In numeric variables, each type has a certain limit. Variables of larger type can take the value of smaller ones. Here, the program will do the conversion itself. For example, the value stored by a variable of type short can be given to a variable of type int or long. Because int type is larger, it will be able to store data in short type.
Short x=100;
int y;
y = x;
This example works fine because the type of variable x is shorter than the type of variable y.
This example also works fine because the float type will be able to store larger numbers than the int type.
However, something like the following will cause an error. Because a variable of type short, int type variables are tried to be a larger value. In this example, don't be fooled by the fact that the value of the variable a (245) is suitable for the short type. What matters is the type of variables, and a variable of type short cannot be given the value of a variable of type int.
Int a=245;
Short b;
b = a;
In the example below, different types of variables are defined and they are converted to each other. It is also stated which ones will work and which ones will not.
byte a;
short b;
int c;
long d;
float x;
a=b; (Error)
b=a; (Works)
c=b; (Works)
c=d; (Error)
d= c ; (Works)
d = x; (Hata)
x=d; (Works)
2- Open Transformation:
In this method, we need to perform the conversion process ourselves. For this, different methods are used depending on the place.
a- Convert method:
It can convert between numeric types as well as convert object type to different types. We can say that it is the most effective among the transformation methods.
int a;
double b = 100.58;
a= Convert.toInt32( b );
In this example, we have converted the value of the variable b to an integer to the variable a.
int sayi1;
sayi1 = Convert.toInt32(Console.ReadLine( ) );
In this example, the value entered by the user is assigned to the variable number1 by converting it to an int.
b- Parse method:
It is used to convert String type values to number type values. For example, we can use this method when entering the value entered by the user in the console into a numeric variable.
int a;
a= int.Parse( Console.ReadLine( ) );
long b = long.Parse(Console.ReadLine( ) );
short c= short.Parse( Console.ReadLine( ) );
float sayi = float.Parse( Console.ReadLine( ) );
string x = "56";
int y = int.Parse( x );
These examples can be multiplied for all number types.
c- toString() method:
It is a method that can convert numbers or different types of variables to string type. With this method, only string type conversion can be done.
int a = 506;
strint b = a.ToString();
We converted variable a to string type and assigned it to variable b.
Note: Console.Write and Console.WriteLine methods convert different types of data to strings and print them to the screen. When using these methods, there is no need to make additional conversions.
d- Casting process:
In some cases, this method can be used. For example, it can convert an object type variable or element to other types. It can also convert double to int, that is, convert between numbers. But it cannot convert a string type to an int type or an int type to a string type.
Double number = 6,789;
int a;
a = (int) buy;
In this example, we have converted the number variable of type double to type int.
Operations between c# variable types, variable conversion, operations with different types of variables and data, how to make variable type conversion, c# convert examples, what is int.parse method
EXERCISES
There are no examples related to this subject.
Read 767 times.