- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
KeyValuePair<int, int>: 8 bytes
KeyValuePair<long, long>: 16 bytes
KeyValuePair<long, int>: 16 bytes (!!)
KeyValuePair<int, long>: 16 bytes (!!)
我希望后两对只需要 8 (long) + 4 (int)
= 12 个字节。为什么占16个?
大小是使用通过 ILGenerator 发出的动态 SizeOf 确定的,如此处所述:Size of generic structure
最佳答案
这是由于data structure Alignment .引用维基百科文章:
Data alignment means putting the data at a memory address equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory. To align the data, it may be necessary to insert some meaningless bytes between the end of the last data structure and the start of the next, which is data structure padding.
由于 int 是 4 个字节,具有 long 的 KeyValuePair 和 int 会导致数据结构未对齐。这将导致性能显着下降。因此,最好在每个记录上“浪费”4 个字节以使数据对齐,从而能够高效地写入和读取。
至于为什么不填充单个整数——不要忘记使用结构/类添加填充变量相对容易——但对于普通值类型,你没有那个结构/类。我猜测所需的额外工作(在 int 周围添加一个虚拟结构以将填充插入其中)不值得性能提升。
这里 32 位和 64 位操作系统之间确实没有区别,它们仍然需要填充和对齐,但填充量可能会有所不同。
在“过去”,您通常必须手动向数据结构添加填充以获得正确的对齐方式,而如今它往往由编译器自动处理。
关于c# - 为什么 KeyValuePair<int, long> 是 16 个字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35153601/
我有一个 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
我是一名优秀的程序员,十分优秀!