C#数组或字典? [英] C# Array or Dictionary?

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

问题描述

我想知道C#数组是否具有不断的访问速度?

我需要在静态数组中存储1000个项目,这将在服务器启动期间被初始化。这个数组将被readonly使用,
,所以数组不会有任何变化。

我应该使用一个简单的C#数组(新的MyClass [])或者替换为Dictionary。



我真的很喜欢C#,并试图了解C#数组访问的工作原理。

可以将它们与c ++数组进行速度比较吗?

解决方案

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



如果你想访问他们通过索引,然后使用数组。 C#中的数组具有不变的访问速度,并且与访问速度非常类似于C ++数组。



然而,字典具有非常快的访问速度( Item属性 接近 O(1)访问时间,但是取决于存储的密钥对GetHashCode的实现有多好)。如果您需要根据键值查找您的项目,而不是按索引查找您的项目,则会改为使用字典。


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.

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.

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.

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#数组或字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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