C#Array或解释? [英] C# Array or Dictionary?

查看:188
本文介绍了C#Array或解释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道的是C#阵列具有恒定的存取速度?结果
我需要存储在静态数组1000个项目,将服务器启动过程中被初始化。这个阵列会被用于只读,
所以会有数组没有变化。结果
我应该使用一个简单的C#阵列(新MyClass的[])或字典来代替。

I wanted to know is C# array has a constant access speed?
I need to store 1000 items in static array, that will be initialized during server startup. This array will be used readonly, so there will be no changes to array.
Should I use a simple C# array (new MyClass[]) or Dictionary instead.

我真的很新的C#,并试图了解C#数组访问的作品。结果
可以通过它们的速度相对于C ++数组?

I am really new to C# and trying to understand how C# arrays access works.
Can they be compared to c++ arrays by speed?

推荐答案

最好的选择取决于你需要如何访问元素。

The best choice depends on how you need to access the elements.

如果您想要通过索引来访问它们,然后使用数组。在C#阵列具有恒定的存取速度,而且是非常类似于一个C ++阵列在速度方面进行访问。

If you want to access them by index, then use an Array. Arrays in C# have constant access speed, and are very similar to a C++ array in terms of speed for access.

词典,然而,有非常快的权限( Item属性<​​/A>的办法的O(1)访问时间,而是取决于如何好存储的密钥对GetHash code实现)的。如果你需要基于一个键值而不是索引来查找您的项目,那么字典将是适当的替代。

Dictionaries, however, have very fast access (the Item property approaches O(1) access time, but depends on how good of an implementation the stored key has for GetHashCode). If you need to lookup your items based on a key value and not by index, then dictionary would be appropriate instead.

这篇关于C#Array或解释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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