数组/列表/集合的变量命名 - C#
我应该如何称呼用某种类型的数组实例化的变量?
可以简单地使用所持有类型的复数形式吗?
IList
或者我应该在名称中附加类似“List”的内容?
IList
另外,这样的循环通常可以接受吗?
foreach(string item in items)
{
//Do something
}
What should I call a variable instantiated with some type of array?
Is it okay to simply use a pluralised form of the type being held?
IList<Person> people = new List<Person>();
or should I append something like 'List' to the name?
IList<Person> personList = new List<Person>();
Also, is it generally acceptable to have loops like this?
foreach(string item in items)
{
//Do something
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是简单的代码约定,因此您应该遵循您的团队习惯的约定。如果您在每个项目中都是单身,请继续使用相同的约定,无论是什么。
LE:循环没问题,尽管这是一个不同的问题。
This is simple code convention, so you should go with what your team is used to. If you are single per project, just keep using the same convention whatever that is.
LE: the loop is ok, though it's a distinct question.
正如之前提到的,这确实非常主观,但是,当您使用这些类时,我会选择智能感知下拉列表中看起来最好的内容,因为这可能是使用您的代码的其他开发人员将进行的第一个交互点。
As mentioned before this is really very subjective but, I'd go for what looks best in the intellisense dropdown when you use the classes as that is probably the first point of interaction other developers using your code will have.
在我合作过的团队中,我们通常认为最好的做法是不区分列表/数组变量和单个对象变量,而仅使用名称的复数形式。
使用
and
即,当您阅读代码时, 会非常困难。
所以你应该选择像
你
已经提到的那样的东西。
你的循环是完全可以接受的。这没什么问题。
编辑:由于注释更改了措辞和变量名称。
In the teams I've worked with, we commonly consider it good practice to not differentiate a list/array variable from a single object variable, just by using the plural form of the name.
I.e. using
and
will be really hard when you read the code.
So you should go for something like
and
as you already mentioned.
Your loop is perfectly acceptable. Nothing wrong with that.
EDIT: Changed the phrasing and variable names due to comments.
就我个人而言,我会选择“人”,但是这是非常主观的,而不是“编码标准”。遵循你最直观的感觉。
是的,你的循环是完全可以接受的
Personally I'd go for "people", however this is highly subjective and not "coding standard". Go with what you feel is most intuitive.
And yes your loop is perfectly acceptable
我个人认为变量名应该描述类型和上下文。
我个人更喜欢 personList :)
所有这些“个人”陈述,我想这只是我个人的感受:)
I personally feel that a variable name should describe the type as well as the context.
I personally like the personList more :)
With all these 'personally' statements, I guess it's just my personal feeling :)
理想情况下,您应该使用复数形式,尽管有时您可能想使用其他形式。只有指导方针,没有硬性规定。
Ideally, you would use the plural, although there are times you might want to use the other form. There are only guidelines, not hard and fast rules.
我不喜欢使用 PersonList,因为它听起来更像是您编写的包含人员而不是变量的类。
I don't like using PersonList since it sounds more like a class you've written to contain persons than a variable.
我想在过去,当没有功能齐全的 IDE 时,使用像
peopleList
这样的名称更合适,因为他们可以通过名称来确定变量的类型。但如今,这不再是问题了。I guess back in old days when there were no fully featured IDEs, using names like
peopleList
were more fitting since they could figure out the type of the variable by its name. But nowadays, that's not an issue anymore.