Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

c# - does valueType.ToString() does a cast on the valueType?

lets say, i have the following code in c#

int x = 0;
x.ToString();

does this internally does a boxing of x? Is there a way to see this happening from visual studio?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In this specific case, you are using a System.Int32 (an int). That type redefines ToString, Equals and GetHashCode, so no boxing.

If you use a struct that doesn't redefine ToString what you'll have is a constrained callvirt to System.Object.ToString(). The definition of constrained:

When a callvirt method instruction has been prefixed by constrained thisType, the instruction is executed as follows:

  • If thisType is a value type and thisType implements method then ptr is passed unmodified as the 'this' pointer to a call method instruction, for the implementation of method by thisType.
  • If thisType is a value type and thisType does not implement method then ptr is dereferenced, boxed, and passed as the 'this' pointer to the callvirt method instruction.

So there isn't boxing if the value type implements ToString and there is boxing if it doesn't implement it... Interesting. I didn't know.

For non-virtual methods like GetType() that are defined in System.Object the value type is always boxed. Just tested with a:

5.GetType();

resulting IL code:

IL_0001: ldc.i4.5
IL_0002: box [mscorlib]System.Int32
IL_0007: call instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...