Naming convention for a C# Dictionary

丶灬走出姿态 提交于 2019-12-03 03:25:52

问题


How do we name a dictionary variable?

Say in my method I have Dictionary<string, List<string>> dictionary;, where the keys of the dictionary are country names and the values are lists of province/state names. How should I rename dictionary?

I know we can create a Country class for this example. But please don't mention this alternative because I'm thinking of good naming convention here.


回答1:


ProvincesByCountry



回答2:


I use mostly one of these:

  • CountryToStatesDictionary
  • CountryToStatesMap
  • CountryToStatesMapping



回答3:


I like XtoYMap or YFromX.




回答4:


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 dict = GetCountryStateMapping();
foreach(var item in dict)
{
  //Something....
}



回答5:


ProvincesByCountry is not explicit enough, as it sounds like mapping countries to provinces one to one. When accessing ProvincesByCountry["Germany"] I'd expectedly assume one value is an object rather than a list of objects.

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.

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. 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.




回答6:


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".



来源:https://stackoverflow.com/questions/4330690/naming-convention-for-a-c-sharp-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!