广泛使用 C Sharp 中的哈希表

发布于 2024-10-27 21:15:58 字数 241 浏览 3 评论 0原文

在我的 tcp 服务器项目中,我需要跟踪要在全局范围内使用的许多对象。我对哈希表上瘾了,目前我在我的项目中使用了大约 10 个哈希表,它们将存储不同类型的对象。

我的问题是,这样可以吗?我知道它是一个相当庞大的数据结构,但它使我的工作变得非常容易,而不是寻求其他选择。例如,我有很多组文本框,我需要跟踪它们。所以我把它们全部放在一个哈希表中并按我喜欢的方式使用它们。

在大型多线程应用程序上工作时,这种方法是否是可接受且正常的编程方式?

In my project which is kind of a tcp server, i need to keep track of a lot of objects to be used in a global scope. I got addicted towards hashtable and currently im using like 10 hashtables in my project which will store different kinds of objects.

My question is, is this alright? i know that its a pretty huge data strucre but it makes my work very easy, rather than going for some other alternative. For instance i have many groups of textboxes which i need to keep track of. so i put them all in a hashtable and use them as i like.

Is this kind of approach an acceptable and normal way to program when working on a large multithreaded application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自我难过 2024-11-03 21:15:58

从您的示例中尚不清楚,但听起来好像您正在使用全局哈希图从程序中的任何点访问您需要的对象(例如界面中的文本框)。

a) 这样的全局访问不是一个好的实践,对于接口对象更是如此。这些应该只能从前端模块访问,你的 TCP 服务器后端不会关心这些。

b) 在“功能组”中排列对象的正确方法是拥有适当的类,而不是 5-10 个哈希图。为什么,在实践中?考虑这两种方法(在类似 python 的伪代码中):

# Interface is a hash-map of interface objects, like text-boxes. 'status' is the key
# in the map for one of those.
Interface['status'].setText("New status!")
# vs:
# Interface is a proper object with its own class, that handles the interface. It has
# all our text-boxes "inside" of it.
Interface.updateStatus("New status!")

现在,假设您改变主意并希望将状态表示为绘图,而不是文本框。使用第二种方法很简单:您只需相应地调整 GUI,并更改 updateStatus 方法的行为方式即可!

使用第一种方法,您现在会遇到混乱对不再存在的文本框的 hahsmap 访问,这些访问遍布您的代码中。

这是一种比哈希图或全局对象更通用的做法 - 它是关于使用具有明确定义的接口的对象,并且可以在内部更改而不影响程序的其余部分

It's not clear from your example, but it sounds as if you're using global hashmaps to access objects you need (such as textbox in an interface) from any point in your program.

a) Global access like that is not good practice, and even less so with interface objects. Those should be accessed only from the front-end module, your TCP server back-end couldn't care less about those.

b) The correct way of arranging objects in a "functional group" is having a proper Class, not 5-10 hashmaps. Why, in practice? Consider these two approaches (in pythonlike pseduo-code):

# Interface is a hash-map of interface objects, like text-boxes. 'status' is the key
# in the map for one of those.
Interface['status'].setText("New status!")
# vs:
# Interface is a proper object with its own class, that handles the interface. It has
# all our text-boxes "inside" of it.
Interface.updateStatus("New status!")

Now, say you change your mind and want to represent the status as a drawing, instead of a text-box. With the second approach that's easy: you just adjust the GUI accordingly, and change the way the updateStatus method behaves!

With the first approach you now have a mess of hahsmap accesses to a text-box that doesn't exist anymore, spread all over your code.

This is a much more general practice than just hashmaps or global objects -- it's about working with objects that have clearly defined interfaces, and that can change internally without the rest of the program being affected.

对你而言 2024-11-03 21:15:58

使用哈希表的最大优点是可以存储任何类型的数据。 HashTable 内部使用字典类型。

如果您存储在哈希表中的所有数据都是同一类型,我建议您使用通用类型。

字典<整数,字符串>或偶数列表<,>类型以获得更好的性能。

The great advantage of using hashtable is that you can store data of any type. Internally HashTable uses Dictionary Type.

If all the data you are storing inside the hashtable is the of the same type i would suggest you to use Generic type.

Dictionary < int, string > or Even List<,> type for better performance.

凹づ凸ル 2024-11-03 21:15:58

只要你的内存消耗很低,我认为没有问题。之后您应该将哈希表迁移到磁盘。要么使用完整的关系数据库,要么简单地使用键值存储之一。你的选择。

I see no issue with it as long as your memory consumption it low. Afterwards you should migrate The hash table to disk. Either use a full flown relational database or simply use one of the key value stores. Your choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文