7

Implicit type conversion vs implicit type conversion in C#?

 2 years ago
source link: https://www.codeproject.com/Questions/5318289/Implicit-type-conversion-vs-implicit-type-conversi
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

See more:

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.

Copy Code
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.
Copy Code
double c = 56.99;

 int d = (int) c; // the same could have been done with Convert.ToInt32(c) or int.Parse(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.
Copy Code
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.
Copy Code
string f = "45";

 int g = Convert.ToInt32(f); // the same could have been done with int.Parse(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.
Copy Code
int h = Convert.ToInt32("55"); // the same could have been done with int.Parse("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.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK