在c#中获取哈希表的大小 [英] Getting the size of a hashtable in c#

查看:204
本文介绍了在c#中获取哈希表的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计我需要你的帮助,我试图在c#中获得哈希表的大小,但它似乎没有那么好用。在java中,您可以通过调用size函数来完成此操作。



Hey guys I need you help, I am trying to get the size of a hashtable in c# but it doesn't seem to work out so well. In java you can do this by calling the size function.

public int size() {
hashmap hasDataTable = new hashmap();           
int liSize = hashDataTable.size();
return liSize;
}





但由于c#hastable没有尺寸功能,我试过这个





but since c# hastable does not have the size function, I tried this

public int size() 
{
  hashTable Data = new hashTable();
  int liSize = sizeof(Data);
  return LiSize;
}





但Visual Studio不会让我编译它说数据是一个字段,但它被用作一个字段键入。



感谢您的帮助



but Visual Studio woun't let me compile it saying "Data is a field but its being used as a type".

thanks for your help

推荐答案

尝试 int liCount = Data.Count



希望这会有所帮助,

Fredrik
Try int liCount = Data.Count.

Hope this helps,
Fredrik


除非您使用非托管代码,否则它不是真正有效的概念。 .NET有自己的内存管理系统,可以使内存中的对象大于其各部分的总和。它与C ++中的不同,其中内存中对象的大小实际上等于它包含的数据量。



你有2种方法可以做到这一点,如果你想知道非托管代码的哈希表的大小,你可以这样做:



Its not really a valid concept here unless you are using unmanaged code. .NET has its own memory management system that can make objects larger in memory than the sum of their parts. Its not the same as in C++ where the size of an object in memory is really equal to the amount of data it contains.

You have 2 ways to do this, if you want to know the size of the hash table for unmanaged code, you can do:

int sizeBytes = System.Runtime.InteropServices.Marshal.SizeOf(Data);





哪个会给出对象的大小,如果它被编组为非托管代码。它不是内存中的大小。



另一种选择是将其序列化为内存流,然后读取流的大小。这仅适用于哈希表中的对象都可序列化的情况。您将不得不遍历它并序列化对象(而不是整个哈希表本身,因为它不可序列化)。



Which will give the size of the object if it were to be marshalled to unmanaged code. Its not really the size in memory.

The other option you have is to serialize it to a memory stream then read the size of the stream. This only works if the objects in your hash table are all serializable. You would have to iterate through it and serialize the objects (not the entire hash table itself since its not serializable).


这篇关于在c#中获取哈希表的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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