简体   繁体   English

C#字典的命名约定

[英]Naming convention for a C# Dictionary

How do we name a dictionary variable? 我们如何命名字典变量?

Say in my method I have Dictionary<string, List<string>> dictionary; 在我的方法中说我有Dictionary<string, List<string>> dictionary; , where the keys of the dictionary are country names and the values are lists of province/state names. dictionary的键是国家名称,值是省/州名称列表。 How should I rename dictionary ? 我该如何重命名dictionary

I know we can create a Country class for this example. 我知道我们可以为这个例子创建一个Country类。 But please don't mention this alternative because I'm thinking of good naming convention here. 但是请不要提到这个替代方案,因为我在这里考虑好的命名约定。

ProvincesByCountry

I use mostly one of these: 我主要使用其中一个:

  • CountryToStatesDictionary CountryToStatesDictionary
  • CountryToStatesMap CountryToStatesMap
  • CountryToStatesMapping CountryToStatesMapping

我喜欢XtoYMapYFromX

ProvincesByCountry is not explicit enough, as it sounds like mapping countries to provinces one to one. ProvincesByCountry不够明确,因为它听起来像是将国家一对一地映射到各省。 When accessing ProvincesByCountry["Germany"] I'd expectedly assume one value is an object rather than a list of objects. 当访问ProvincesByCountry [“Germany”]时,我希望假设一个值是一个对象而不是一个对象列表。

My personal pattern is similar: 我的个人模式类似:

[Plural of a noun describing the value]By[Singular of a noun describing the key]

However, if a noun describing the value is plural by its nature, then I use the postfix arrays , or lists , as in English you can't really "pluralise" a plural. 但是,如果描述该值的名词本质上是复数,那么我使用后缀数组列表 ,就像在英语中一样,你不能真正“复数”复数。 I personally always stick to arrays , regardless of the actual implementation of IEnumerable or IEnumerable< T> I'm using, be that List , or Array or whatever. 我个人总是坚持数组 ,无论IEnumerable的实际实现或IEnumerable <T>我正在使用,是List ,或Array等等。

In your case it turns to: 在你的情况下,它转向:

ProvinceArraysByCountry

Tells what it is with scientific precision. 用科学的精确度来说明它是什么。

I apply this rule recursively if there are dictionaries as values. 如果有字典作为值,我会递归地应用此规则。 The order of accessing then goes in reverse to the order of words in the name. 然后,访问顺序与名称中单词的顺序相反。 Imagine you add planets: 想象一下你添加行星:

ProvinceArraysByCountryByPlanet["Earth"]["Germany"][0] = "Bavaria"
ProvinceArraysByCountryByPlanet["Earth"]["Germany"][1] = "Rhineland-Palatinate"

And finally the last little stroke here. 最后这里是最后一点点。 If such dictionary maps object properties and the objects themselves, then I leave out the word describing the object in the key section. 如果这样的字典映射对象属性和对象本身,那么我省略了在关键部分描述对象的单词。 Here is what I mean: 这就是我的意思:

NodesByIndex[node.Index] = node; // - Do
NodesByNodeIndex[node.Index] = node; // - Don't

I use this pattern unconditionally which is good as it leaves absolutely no room for guess. 我无条件地使用这种模式,因为它绝对没有猜测空间。 Con is it generates fairly long names sometimes. Con有时会产生相当长的名字。 But I have no idea how to have always explicit but always short names. 但我不知道如何总是明确但总是短名。 You always have to compromise. 你总是要妥协。 And it's of course a matter of taste. 这当然是品味问题。

This pattern doesn't work (or at least you'd break your brain) when keys are also dictionaries or when you have list of dictionaries of lists of dictionaries or some other crazy exotic stuff. 当密钥也是字典或者你有字典列表或其他一些疯狂的异国情调的字典列表时,这种模式不起作用(或者至少你会打破你的大脑)。 But I don't remember having that many levels of nesting, so I'm happy with it. 但我不记得有那么多层次的筑巢,所以我很高兴。

Naming is always contextual. 命名始终是上下文。 So in this specific case some name specifying the country to state mapping is appropriate. 因此,在这种特定情况下,指定国家/地区映射的某个名称是合适的。

if this was just a device for a loop within a larger context and then discarded, I normally just go with a short temp type var like... 如果这只是一个在更大的上下文中循环然后丢弃的设备,我通常会选择一个短临时类型的var ...

var dict = GetCountryStateMapping();
foreach(var item in dict)
{
  //Something....
}

provinces, provinceMap, provinceDictionary 省,省地图,省词典

All come to mind. 一切都浮现在脑海中。 I like provinceMap my self. 我喜欢省地图我自己。 If it's a member field I would add an "m_" prefix, as in "m_provinceMap". 如果它是一个成员字段,我会添加一个“m_”前缀,如“m_provinceMap”中所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM