Hello world, i'm new here. I read this site is similar stack overflow but with much nicer people or so i've read. So I thought I'd give it a try :)
With that being said, the bottom explanation is my current understanding on the matter. And even with reading
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions I am still confused. Is my understanding on the topic correct or perhaps I was incorrect in some areas?
#1
In the bottom implicit conversion example, the data type of a "copy" (not the original value) of the value assigned to variable
a is implicitly converted from type
int
to type
double
. The result is then assigned to variable
b. Thus, the value that was assigned to variable
a is still of type
int
while the value (a totally new value produced due to implicit conversion) assigned to variable
b is of type
double
.
int a = 6;
double b = a;
#2
In the bottom explicit conversion example, the data type of a "copy" (not the original value) of the value assigned to variable
c is explicitly converted from type
double
to type
int
. The result is then assigned to variable
d. Thus, the value that was assigned to variable
c is still of type
double
while the value (a totally new value produced due to explicit conversion) assigned to variable
d is of type
int
.
double c = 56.99;
int d = (int) c;
#3
In the bottom explict conversion example, the data type of the orginal value 78.44 (not a copy of the value because it was not assigned to any variable) is converted from type
double
to type
int
. The result is then assigned to variable
e.
int e = (int) 78.44;
#4
In the bottom explicit conversion example, the data type of a "copy" (not the original value) of the value referenced to variable
f is explicitly converted from type
string
to type
int
. The result is then assigned to variable
g. Thus, the value that was referenced to variable
f is still of type
string
while the value (a totally new value produced due to explicit conversion) assigned to variable
g is of type
int
.
string f = "45";
int g = Convert.ToInt32(f);
#5
In the bottom explict conversion example, the data type of the orginal value "55" (not a copy of the value because it was not referenced by any variable), is converted from type
string
to type
int
. The result is then assigned to variable
h.
int h = Convert.ToInt32("55");
IN SIMPLE WORDS
What I am saying is that when a variable is part of the type conversion code statement, the data type of orginal value assigned to it (when value is of Value type) or referenced by it (when value is of Reference type) is not converted. But it is the "copy" of the orginal value that was assigned to or referenced by the variable, whose data-type is converted to whatever the desired type. As explained and shown in #'s 1, 2 and 4.
Furthermore, what I am saying is that when a variable IS NOT part of the type conversion code statement, the data type of actual value (no such thing as a copy of the value in this case) dealt with, is to whatever the desired type. As explained and shown in #'s 3 and 5.
What I have tried:
I understand the end result of implicit and explcicit type conversion in C# but I am confused about what goes on under the hood. Online resources do not state much on the matter.