拳击在调用toString时结构() [英] boxing on structs when calling ToString()

查看:130
本文介绍了拳击在调用toString时结构()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我如果存在以下情况在C#中实际发生的事情经常在想



如果我有一个结构,但我没有明确覆盖任何从对象派生,如toString(),GetHashCode的()等方法那么,如果我宣布我的结构类的本地实例,并调用'的ToString()'就可以了,将我的结构得到盒装即会在CLR将其转换隐堆中的对象,然后调用toString()?或者是足够聪明的知道,有该结构没有实现,而忽略它?



 公共结构的Vector2D 
{
公众持股量m_x;
公众持股量m_y;


......等等
}


无效SomeFunc()
{
的Vector2D AVEC =新的Vector2D();
Console.WriteLine(aVec.ToString()); //< - 不AVEC在这里得到盒装?
.....
}



==编辑 - 更新==
迈赫达德的链接MSDN ,同时又是有用的一直困惑性能稍微我。
我还要举,看看有没有人能够拆散这对我来说




当一个callvirt方法指令
有受限于
thisType被前缀,该指令被执行
,如下所示:



如果thisType是引用类型(
反对值类型),那么PTR是
复引用并作为'这个'
指向方法的callvirt过去了。



如果thisType是一个值类型和
thisType实现方法,那么PTR是
通过未修改的'这个'
指针调用方法指令,

thisType的实现方法。



如果thisType是值类型和
thisType不实现方法
,则PTR取消引用,盒装,和
因为这个指针
callvirt方法指令过去了。




这是否意味着,如果我没有明确我的结构类型实施的ToString(),它会落入最后一个case,得到盒装?还是我错理解它是什么地方?


解决方案

如果 thisType 是值类型和
thisType 未实现方法
,则PTR取消引用,盒装,和
通过因为这个指针
callvirt方法指令。




法于<$ C $定义只能出现最后一种情况C>对象,
值类型枚举,而不是覆盖$ b通过 thisType $ b。在这种情况下,拳击
导致原始对象
键作出的副本。




答案是肯定的,值类型为盒装。这就是为什么它总是覆盖的ToString()自定义结构。


一件好事

Hi I've often wondered if the following scenario actually happens in c#

If I have a struct but I don't explicitly override any of the methods that derived from object such as ToString(), GetHashCode(), etc then if I declare a local instance of my struct class and call 'ToString()' on it, would my struct get boxed i.e would the CLR convert it implicitly to an object on the heap and then call ToString()? Or is it clever enough to know that there's no implementation for that struct and ignore it?

i.e

public struct Vector2D
{
    public float m_x;
    public float m_y;


    ...... etc
}


void SomeFunc()
{
  Vector2D aVec = new Vector2D();
  Console.WriteLine(aVec.ToString()); // <-- does aVec get boxed here?
  ..... 
}

== Edit - Update== Mehrdad's link to MSDN, whilst being useful has confused me slighly. I'll quote and see if any one can unpick this for me

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

If thisType is a reference type (as opposed to a value type) then ptr is dereferenced and passed as the 'this' pointer to the callvirt of method.

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 does that mean that if I don't explicitly implement ToString() on my struct type that it will fall into the last case and get boxed? Or am I mis-understanding it somewhere?

解决方案

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.

This last case can occur only when method was defined on Object, ValueType, or Enum and not overridden by thisType. In this case, the boxing causes a copy of the original object to be made.

The answer is yes, the value type is boxed. This is why it is always a good thing to override ToString() on custom structs.

这篇关于拳击在调用toString时结构()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆