如何给字典命名?

发布于 2024-10-25 05:50:41 字数 811 浏览 2 评论 0原文

.NET 中是否有命名关联数组(例如 Dictionary)的约定/准则?

例如,是否有更好的方法来命名 dict

var dict = new Dictionary<Foo, Bar>();

我使用/见过的一些名称:

foosToBars
mapFromFooToBar
barsByFoo
barsForFoos

我还看到 BCL 中的一些类型选择“更好”的听起来不直接的名称揭示键和值应该代表什么,例如此方法

对于多重映射怎么样(例如ILookup)?

编辑

也许我的例子有点糟糕;我并不是想关注键和值的类型

如果是的话怎么样:

// key = Name of person, value = Age of person
var dict = new Dictionary<string, int>();

样本更新为:

namesToAges
mapFromNameToAge
agesByName
agesForNames

Are there any conventions / guidelines for naming associative arrays (e.g. Dictionary<TKey, TValue>) in .NET?

For example, is there a better way to name dict in:

var dict = new Dictionary<Foo, Bar>();

Some names I've used / seen:

foosToBars
mapFromFooToBar
barsByFoo
barsForFoos

I've also seen some types in the BCL choosing 'nicer' sounding names that don't directly reveal what the key and value are supposed to represent, such as in this method.

How about for multimaps (e.g. ILookup<TKey, TElement>)?

EDIT:

Perhaps my example was slightly poor; I didn't mean to focus on the types of the key and value.

How about if it was:

// key = Name of person, value = Age of person
var dict = new Dictionary<string, int>();

With the samples updated to:

namesToAges
mapFromNameToAge
agesByName
agesForNames

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

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

发布评论

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

评论(1

暮年慕年 2024-11-01 05:50:43

我会尝试按照您链接到的示例进行操作,在该示例中,您不会根据字典包含的内容来命名字典,而是根据其用途来命名它。很快有人就会确定“dict”是一个将 Foo 键映射到 Bar 值的字典,但我想知道的是为什么你正在这样做。

I'd try to follow the example you linked to, where you don't name the dictionary after what it contains but you name it for what it's for. It doesn't take long for someone to determine that "dict" is a dictionary that maps Foo keys to Bar values, but what i'd want to know is why you're doing that.

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