数组/列表/集合的变量命名 - C#

发布于 2024-08-31 16:19:48 字数 367 浏览 3 评论 0原文

我应该如何称呼用某种类型的数组实例化的变量?

可以简单地使用所持有类型的复数形式吗?

IList; people = new List();

或者我应该在名称中附加类似“List”的内容?

IList; personList = new List();

另外,这样的循环通常可以接受吗?

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 技术交流群。

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

发布评论

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

评论(8

空宴 2024-09-07 16:19:48

这是简单的代码约定,因此您应该遵循您的团队习惯的约定。如果您在每个项目中都是单身,请继续使用相同的约定,无论是什么。

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.

愿与i 2024-09-07 16:19:48

正如之前提到的,这确实非常主观,但是,当您使用这些类时,我会选择智能感知下拉列表中看起来最好的内容,因为这可能是使用您的代码的其他开发人员将进行的第一个交互点。

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.

雾里花 2024-09-07 16:19:48

在我合作过的团队中,我们通常认为最好的做法是不区分列表/数组变量和单个对象变量,而仅使用名称的复数形式。

使用

object horse = new object();

and

List<object> horses = new List<object>;

即,当您阅读代码时, 会非常困难。
所以你应该选择像

object horse = new object();

List<object> PackOfHorses = new List<Object>();

已经提到的那样的东西。

你的循环是完全可以接受的。这没什么问题。

编辑:由于注释更改了措辞和变量名称。

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

object horse = new object();

and

List<object> horses = new List<object>;

will be really hard when you read the code.
So you should go for something like

object horse = new object();

and

List<object> PackOfHorses = new List<Object>();

as you already mentioned.

Your loop is perfectly acceptable. Nothing wrong with that.

EDIT: Changed the phrasing and variable names due to comments.

月光色 2024-09-07 16:19:48

就我个人而言,我会选择“人”,但是这是非常主观的,而不是“编码标准”。遵循你最直观的感觉。

是的,你的循环是完全可以接受的

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

这个俗人 2024-09-07 16:19:48

我个人认为变量名应该描述类型和上下文。
我个人更喜欢 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 :)

飞烟轻若梦 2024-09-07 16:19:48

理想情况下,您应该使用复数形式,尽管有时您可能想使用其他形式。只有指导方针,没有硬性规定。

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.

爺獨霸怡葒院 2024-09-07 16:19:48

我不喜欢使用 PersonList,因为它听起来更像是您编写的包含人员而不是变量的类。

I don't like using PersonList since it sounds more like a class you've written to contain persons than a variable.

递刀给你 2024-09-07 16:19:48

我想在过去,当没有功能齐全的 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.

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