- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
最近在做其他事情时,我在 KeyValuePair<TKey, TValue>.ToString()
implementation 上遇到了一段有点奇怪的代码.
public override string ToString()
{
StringBuilder stringBuilder = StringBuilderCache.Acquire(16);
stringBuilder.Append('[');
if (this.Key != null)
{
StringBuilder arg_33_0 = stringBuilder;
TKey tKey = this.Key;
arg_33_0.Append(tKey.ToString());
}
stringBuilder.Append(", ");
if (this.Value != null)
{
StringBuilder arg_67_0 = stringBuilder;
TValue tValue = this.Value;
arg_67_0.Append(tValue.ToString());
}
stringBuilder.Append(']');
return StringBuilderCache.GetStringAndRelease(stringBuilder);
}
跳过 StringBuilderCache
类的使用(这是 .NET 本身性能改进的一个很好的例子)我有一个问题:
为什么是
if (this.Key != null)
{
StringBuilder arg_33_0 = stringBuilder;
TKey tKey = this.Key;
arg_33_0.Append(tKey.ToString());
}
更好
if(this.Key != null)
{
stringBuilder.Append(this.Key.ToString());
}
?
分配新的局部变量而不是直接使用实例有什么好处?
最佳答案
根据 Reference Source 的原始 C# 代码是:
public override string ToString() {
StringBuilder s = StringBuilderCache.Acquire();
s.Append('[');
if( Key != null) {
s.Append(Key.ToString());
}
s.Append(", ");
if( Value != null) {
s.Append(Value.ToString());
}
s.Append(']');
return StringBuilderCache.GetStringAndRelease(s);
}
根据 ILspy 的方法的 IL 代码是:
.method public hidebysig virtual
instance string ToString () cil managed
{
.custom instance void __DynamicallyInvokableAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x5f79c
// Code size 125 (0x7d)
.maxstack 2
.locals init (
[0] class System.Text.StringBuilder,
[1] !TKey,
[2] !TValue
)
IL_0000: ldc.i4.s 16
IL_0002: call class System.Text.StringBuilder System.Text.StringBuilderCache::Acquire(int32)
IL_0007: stloc.0
IL_0008: ldloc.0
IL_0009: ldc.i4.s 91
IL_000b: callvirt instance class System.Text.StringBuilder System.Text.StringBuilder::Append(char)
IL_0010: pop
IL_0011: ldarg.0
IL_0012: call instance !0 valuetype System.Collections.Generic.KeyValuePair`2<!TKey, !TValue>::get_Key()
IL_0017: box !TKey
IL_001c: brfalse.s IL_0039
IL_001e: ldloc.0
IL_001f: ldarg.0
IL_0020: call instance !0 valuetype System.Collections.Generic.KeyValuePair`2<!TKey, !TValue>::get_Key()
IL_0025: stloc.1
IL_0026: ldloca.s 1
IL_0028: constrained. !TKey
IL_002e: callvirt instance string System.Object::ToString()
IL_0033: callvirt instance class System.Text.StringBuilder System.Text.StringBuilder::Append(string)
IL_0038: pop
IL_0039: ldloc.0
IL_003a: ldstr ", "
IL_003f: callvirt instance class System.Text.StringBuilder System.Text.StringBuilder::Append(string)
IL_0044: pop
IL_0045: ldarg.0
IL_0046: call instance !1 valuetype System.Collections.Generic.KeyValuePair`2<!TKey, !TValue>::get_Value()
IL_004b: box !TValue
IL_0050: brfalse.s IL_006d
IL_0052: ldloc.0
IL_0053: ldarg.0
IL_0054: call instance !1 valuetype System.Collections.Generic.KeyValuePair`2<!TKey, !TValue>::get_Value()
IL_0059: stloc.2
IL_005a: ldloca.s 2
IL_005c: constrained. !TValue
IL_0062: callvirt instance string System.Object::ToString()
IL_0067: callvirt instance class System.Text.StringBuilder System.Text.StringBuilder::Append(string)
IL_006c: pop
IL_006d: ldloc.0
IL_006e: ldc.i4.s 93
IL_0070: callvirt instance class System.Text.StringBuilder System.Text.StringBuilder::Append(char)
IL_0075: pop
IL_0076: ldloc.0
IL_0077: call string System.Text.StringBuilderCache::GetStringAndRelease(class System.Text.StringBuilder)
IL_007c: ret
} // end of method KeyValuePair`2::ToString
如您所见,只有一个 StringBuilder 类型的局部变量。
变量 arg_33_0
和 arg_67_0
是您使用的反编译器的产物;它们既不在原始 C# 代码中,也不在编译后的 IL 代码中。
关于c# - KeyValuePair<TKey, TValue>.ToString 实现细节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18172596/
我有一个 List> .我想知道 KeyValuePair谁拥有最大值 Key . 但是,我想获取 key 的值。不仅是 key 。 我能做到: int myValue = myListe.Max(
C#我在我的主 KeyValuePair 中使用 KeyValuePair 作为键。这是我的问题: 如果我有“KeyValuePair”,声明如下: KeyValuePair varName = ne
在 C# 中,如何将 KeyValuePair 转换为 KeyValuePair?我尝试了以下但它不起作用: public static KeyValuePair DoTheCast(KeyValue
以这种糟糕的数据结构结束: List>> 它不太可能变得很大(我估计 > 这很容易在 .NET 2.0 中编写 - 它基本上只是一个值的三元组,而不是在 KeyValuePair 中有 2 个。不过,
查看System.Collections.Generic.Dictionary , 它清楚地实现了 ICollection> ,但没有所需的“void Add(KeyValuePair item)”功
是否可以使用自定义结构来代替 KeyValuePair? 而不是 For Each kvp As KeyValuePair(Of class1,class2) In myDict Dim c1 A
我们如何验证 (.NET 2) 如果 KeyValuePair是否已分配值? Dim pair as KeyValuePair(Of A, B) = Nothing if pair.??? Then
我的 list返回 显然。如何在我的 KeyValuePair 中附加第二个字符串并添加我从第二次数据库调用返回的 ID? // Database calls here KeyValu
编辑:文化 这些内部操作表明不变文化 OrdinalIgnoreCase最合适。 KeyValuePair 是密封的。如果我有 var a = KeyValuePair("foo",1)和 var b
我正在用 C#/.NET 4.5 编写一个应用程序,我有一个定义如下的字典: Dictionary d = new Dictionary(); 我的整数键是连续且唯一的,除了空字符串外,我的值也将是唯
我正在执行 LINQ 查询以从数据库、年龄和日期中获取 2 个字段中的所有值。我想在图表上显示 5 个最高年龄的数字。为此,我试图将年龄和日期存储在 2 个单独的列表 ageList 和 dateLi
我有一个从数据库中填充的字典。 如果只返回一条记录,如何访问KeyValuePair? 我有这个代码: Dictionary CandidatePlatypiDict = PlatypusID > 0
假设我有 List> d 我知道字符串,但我想找到它的整数。如何在此列表中找到键值对? 最佳答案 你会这样写: var result = d.Where(kvp => kvp.Value == "s
我有一个具有 KeyValuePair 类型属性的对象。 我想从数据库中读取一些数据并将结果存储在这个 KeyValuePair 类型字段中。 myObject.KeyValuePairs = ctx
我有一个 IEnumerable对于具有 string 的实体和一个 int成员。 我需要将其转换为 KeyValuePair 的数组反之亦然。 它因转换错误而失败。 [DataContract] p
我有一个 IEnumerable> keyValueList 类型的对象, 我正在使用 var getResult= keyValueList.SingleOrDefault(); if(getR
我正在 build 一个 Dictionary带 key 型KeyValuePair .使用时KeyValuePair.Create内Dictionary初始化程序它不需要模板类型并且它正确地推断了类
我有一个列表 KeyValuePair在 C# 中格式为 KeyValuePair .我想从列表中删除具有重复值的项目。 具有 {X,Y} 的 Point 对象坐标。 示例数据: List> Data
我有这个代码: List>> aux = retEmpty.ToList(); aux.ForEach(x => ret.Add(x.Key, x.Value)); 我需要按第一个元素(“string
我有一个方法需要以下内容: public static List ParetoBuildBySum(List> inputData) 我有以下 linq 查询,并希望传入 KeyValueP
我是一名优秀的程序员,十分优秀!