- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
比方说,我在 c# 中有以下代码
int x = 0;
x.ToString();
这是否在内部对 x 进行装箱?有没有办法从 visual studio 中看到这种情况?
最佳答案
在此特定情况下,您使用的是 System.Int32
(int
)。该类型重新定义了 ToString
、Equals
和 GetHashCode
,因此没有装箱。
如果您使用不重新定义 ToString
的 struct
,您将拥有一个 constrained callvirt
到 System.对象.ToString()
。 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.
因此,如果值类型实现了 ToString
,则没有装箱,如果没有实现,则有装箱……有趣。我不知道。
对于 System.Object
中定义的非虚拟方法,如 GetType()
,值类型始终被装箱。刚刚测试过:
5.GetType();
生成的 IL 代码:
IL_0001: ldc.i4.5
IL_0002: box [mscorlib]System.Int32
IL_0007: call instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
关于c# - valueType.ToString() 是否对 valueType 进行强制转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18615648/
比方说,我在 c# 中有以下代码 int x = 0; x.ToString(); 这是否在内部对 x 进行装箱?有没有办法从 visual studio 中看到这种情况? 最佳答案 在此特定情况下,
C# 不允许结构派生自类,但所有 ValueType 都派生自 Object。这个区别在哪里? CLR 如何处理这个问题? 最佳答案 C# doesn't allow structs to deriv
我正在尝试向泛型方法添加约束,以便它检查 ValueTypes、Strings 或 Nullable 值类型。 问题在于: 值类型是 struts 字符串是不可变的引用类型 可为空的是值类型,但不会在
我正在做一项不允许使用任何 STL 容器的家庭作业。我对 LinkedList 的实现是用指针链接在一起的节点集合。我有另一个名为 ContinuousList 的类,它有一个数据成员 LinkedL
引自MSDN Link for ValueType Class In cases where it is necessary for a value type to behave like an ob
以下打印等于: struct A { int x; public A(int _x) { x = _x; } public int Y { get
我构建了一个框架,该框架允许根据哪个列是主排序列对表中的报告数据进行级联排序。它在大多数情况下都有效,但在一个特定但重要的情况下除外:当字段的属性是值类型时。我收到以下错误消息: System.Arg
我有一个方法,它返回给定的类型: T foo(Class valueType) {...}; String s = foo(Java.lang.String.class); 我正在尝试发送泛型类型作为
是否可以在不编写特定于类型的克隆代码的情况下克隆一个已知为盒装 ValueType 的对象? 一些引用代码 List values = new List {3, DateTime.Now, 23.4M
几天前我需要切换一个bool,结果我这样做了: IsVisible = !IsVisible; 我发现这是归档该功能的最简单方法。但在像上面的例子那样做之前,我尝试了一些不同的方法。 主要是关于使用扩
我的理解是 .Net 中的每个新线程分配 1MB of stack space .我的进一步理解是值类型存储在堆栈而不是堆中...... 所以我的问题是这样的;这种行为是否意味着任何 ValueTyp
如果您查看 .NET Nullable 的文档,您会看到: struct Nullable 请注意,它是一个结构,而不是一个类。 好像是struct Nullable 不是 ValueType,
执行这段代码后我感到很困惑,其中字符串的行为似乎就像它们是值类型一样。我想知道赋值运算符是否正在对字符串的相等运算符等值进行操作。 这是我用来测试这种行为的一段代码。 using System; na
问题应该是不言自明的。我有一个 datagridview,其中有一列,其单元格在用户输入时应仅接受正整数。 那么我怎样才能设置这样的东西: dgv.Columns[i].Val
我在许多文章中读到所有值都是从System.ValueType派生的类型。但经过一番调查后,我发现所有值类型要么是 struct 要么是 enum。 众所周知,struct 不支持继承。那么值类型是如
如果我将一个简单的值类型(例如 int)分配给 ValueType 类型的属性,那么该值是否被装箱? 例如,如果我这样做: int i = 4; ValueType test = i; 值会被装箱吗?
System.ValueType 是 class , 然而所有值类型都是结构。 如果我像这样创建一个 ValueType 的实例: System.ValueType foo = 5; ...是保存在堆
考虑这个类: public class Foo { public Int32 MyField; } 我猜“MyField”成员不在线程堆栈上,因为它可以被多个线程访问,它肯定在托管堆中,但这是
结构体只是一个继承自 System.ValueType 的类,对吗? 关键字“struct”是否只是编写一个类的语法糖:名称后有 System.ValueType? 如果它只是一个类,那么不是所有的类
当我尝试定义一个继承自 System.ValueType 或 System.Enum 类的类时,出现错误: Cannot derive from special class System.ValueT
我是一名优秀的程序员,十分优秀!