如何给字典命名?
.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试按照您链接到的示例进行操作,在该示例中,您不会根据字典包含的内容来命名字典,而是根据其用途来命名它。很快有人就会确定“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 toBar
values, but what i'd want to know is why you're doing that.