Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
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.

C#
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.

C#
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.

C#
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.

C#
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.

C#
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.
Posted
Updated 22-Nov-21 9:04am
v4
Comments
[no name] 22-Nov-21 11:31am    
https://referencesource.microsoft.com/
https://github.com/dotnet

The "source" is out there. You want to write whatever based on "opinions"?
Richard MacCutchan 22-Nov-21 11:36am    
In every case the original value or variable is left as is. The converted value is stored in the new variable. You can think of it as a copy operation, where the conversion takes place during the copy.
OriginalGriff 22-Nov-21 11:42am    
I think I recognise this poster ... let's hope he can remain civil for a change.
Richard MacCutchan 22-Nov-21 11:49am    
I think you may be correct.
OriginalGriff 25-Nov-21 4:00am    
Yeah, he just replied - same troll as before ... :sigh:

Still, if he remains civil he can stay. He might learn something, unlikely though it seems from his history.

1 solution

Like Richard said, every original value is left untouched, even the constant literals you supplied as arguments. In a conversion, a new value is created of the type required, copying the required bits from the appropriate representation fields of the original values.

*
 
Share this answer
 
Comments
DayTimeOwl 22-Nov-21 14:58pm    
Hello thanks for adding a solution. Your solution did make me realize a couple of mistakes I made in terms of the way I explained it. And if you can confirm if my undestanding is now correct? Thanks in advance.

So from what I gather in #'s 1 and 2 a copy of the original value is created, the data-type of the copy is converted to the desired type.

In #3 the data-type of the orginal value (not referring to a copy of it) is converted to the desired type.

In #4 a copy of the reference to string literal "45" is passed to static method ToInt32. A totally new value is created of type Int32 (based on that string literal, in which a copy of its reference that was passed to the static method) and is returned.

In #5 a copy of the reference to string literal "55" is passed to static method ToInt32. A totally new value is created of type Int32 (based on that string literal, in which a copy of its reference was passed to the static method) and is returned.

Is my new understand of the above correct?
Dave Kreskowiak 22-Nov-21 16:15pm    
#1 and #2 are OK, though there is no such thing as a "conversion". Bits are copied from one memory location or CPU register to another and the result is what is referenced by the variable.

#3. In a compiled language, like C#, the compiler sees the cast as a "reinterpret this value" as another type. The result is a constant literal value of the result of the reinterpretation, you example actually becomes:
    int e = 78;


#4. Correct.

#5. Correct.
DayTimeOwl 22-Nov-21 17:07pm    
Your explanation of #3 makes total sense to me now.

However, I think you might made a mistake in your explanation of #1 and #2. The reason why I say this is becauase in your explanation you said "Bits are copied from one memory location or CPU register to another and the result is what is referenced by the variable."

But in #1 and #2 it was dealing with literal values of Value type. So while you said "and the result is what is referenced by the variable"... isn't it actually... "and the result is what is assigned to the variable."

From what I do is that a value of reference type is referenced by a variable while a value of Value type is assigned to a variable.
Dave Kreskowiak 22-Nov-21 17:10pm    
Variables aren't really a thing. They're just a memory address (or pointer.) Value types are allocated on the stack while reference types are allocated on the heap. The variable is just pointing at a chunk of memory.
DayTimeOwl 25-Nov-21 3:13am    
I don't fully understand what you mean. But i'll look into it. Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900