简体   繁体   中英

Is my undestanding of implicit type conversion and explicit type conversion correct?

My current understanding of implicit type conversion and explicit type conversion in C# programming can be found below.

#1

In the implicit conversion example below, 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 . The type of the original value was not altered. The type of the copy was altered.

 int a = 6;

 double b = a;

#2

In the explicit conversion example below, 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 . The type of the original value was not altered. The type of the copy was altered.

 double c = 56.99;

 int d = (int) c; 

#3

In the explicit conversion example below, the data type of the original 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 . The type of the original value was altered.

 int e = (int) 78.44; 

#4

In the example explicit conversion example below, the a copy of the original value 78.44 which is of type double is passed to static method ToInt32() , within that method a new value is created of type Int32 and is returned. The result is then assigned to variable f . The type of the original value was not altered. The type of the copy of the original value was also not altered. A whole new value of the desired type was created and returned by the method invocation.

 int f = Convert.ToIn32(78.44); 

#5

In the explicit conversion example below, a "copy" of the reference to (not a copy of the original value) the original value "45" (which was referenced by a variable) is passed to static method Int32() , within that method a new value is created of type Int32 and is returned. The result is then assigned to variable h . The type of the original value was not altered. A copy of the original value was never created. A whole new value of the desired type was created and returned by the method invocation.

 string g = "45";

 int h = Convert.ToInt32(f);  

#6

In the explicit conversion example below, a "copy" of the reference to (not a copy of the original value) the original value "55" ( which was not not referenced by any variable) , is passed to static method Int32() , within that method a new value is created of type Int32 and is returned. The result is then assigned to variable i . A copy of the original value was never created. The type of the original value was not altered. A whole new value of the desired type was created and returned by the method invocation.

 int i = Convert.ToInt32("55"); 

Question: Is my understanding on the matter correct?

I would appreciate the feedback.

One by one:

In:

int a = 6;
double b = a;

The integer 6 is assigned to the variable a . Then, in the second line, an implicit conversion operator (to double) is applied to the variable a . This results in a temporary that is assigned to the variable b . The literal 6 is unchanged, as is the variable a . In the end, b == 6.0

In

double c = 56.99;
int d = (int) c; 

The literal value 56.99 (a double) is assigned to the variable c . There is no implicit conversion from double to int , but there is an explicit conversion. So the (int) cast invokes that explicit conversion, creating a temporary value and that value is assigned to d . In the end, d == 56

One thing to note in these two examples is that the compiler and JITter can optimize the code as long as the meaning of the code is preserved. A release build of your code wouldn't have any evidence of the variables a or c , they'd just get optimized away, with the resulting code looking a lot like the next example.

This case is basically the same, without the use of an intermediate variable:

int e = (int) 78.44; 

The literal double value 78.44 is explicitly converted to an int . That int intermediate value is assigned to e .

As I said in the comments, when you do an assignment to a variable of value type, the effect is a copy of the value to a variable (which is different from how reference type assignment works). When you pass a value type to a function through a method parameter, the effect is the same as an assignment.

The rest of your examples are different from above. In:

int f = Convert.ToInt32(78.44); 
string g = "45";
int h = Convert.ToInt32(g); 
int i = Convert.ToInt32("55"); 

You are calling a function, not invoking a conversion operator. Calling a function isn't really the same as using an implicit or explicit conversion operators. Functions have parameters and return types. If you have a Func<T, TR> is can be effectively a conversion from T to TR , but not really (consider a predicate, a Func<T, bool> ; it doesn't really convert type T to a boolean).

So, in the cases you discuss, int f = Convert.ToInt32(78.44) simply calls Convert.ToInt32(double) . In doing so, the literal double value is copied to the first (and only) parameter of the method. Then the method is called and that method consumes the value (perhaps through value assignment). It returns an integer back. The value of that integer is copied to the variable f .

In

string g = "45";
int h = Convert.ToInt32(g); 

The first statement copies a reference to the string "45" (which is likely interned ) to the variable g . Remember that strings are immutable , so the function cannot change the underlying representation of the string that g refers to. Then that reference is copied to a parameter of Convert.ToInt32(string) . Eventually, that function returns an integer value that is copied to the variable h .

Your last example: int i = Convert.ToInt32("55") is basically the same, but without the intermediate variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM