Variable naming for arrays/lists/collections - C#
Solution 1
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.
Solution 2
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.
Solution 3
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
Solution 4
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.
Solution 5
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.
David Neale
Updated on June 16, 2022Comments
-
David Neale almost 2 years
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 }
-
Joel Mueller about 14 yearsAs you're the only person who has claimed "best practice" rather than "this is just my opinion," I get to disagree with you. Citation needed on your first sentence, because that doesn't look like a best practice at all. Not even the original version of Hungarian notation called for the data type to be part of the variable name. In addition, since when is PascalCase a best practice for variable names?
-
Nikos Steiakakis about 14 yearsPerhaps my phrasing wasn't all that correct. By "Generally it is considered best practice" I actually meant that generally it's considered 'good'practice to not just use the plurar of a word ( horse, horses) since it is harder to observe when reading the code. I didn't provide citation, because I have not read/heard in a single place. It's just experience. As for the data type, I did not say he should use it, I just used var names simillar to the OP's question (he used personsList). In the example with the horse I just mentioned, I would go for horse - packOfHorses instead of horse-horses
-
John Saunders about 14 yearsI've never before heard anyone say that using the plural for a list makes code harder to read.
-
Nikos Steiakakis about 14 years@John Saunders So you say that it is just as easy for the eye to notice a single letter difference between two variable names? I'm saying that as far as readability is concerned, not in terms of undrestanding what it means. Especially when both the single item variable and list/collection variable are used within the same code listing.
-
John Saunders about 14 yearsI can speak only as a native English speaker - when variables are named after real things, I tend to read the code as prose. In that case, I find it natural to notice the difference between singular and plural.
-
RenniePet about 12 yearsI like your suggestions, but the nit-picker in me forces me to point out that a group of horses is called a "herd", not a "pack".
-
atconway over 9 yearsI thought the plural form of 'horse' was 'horseys' :P
-
Ola Ström about 4 yearsThe suggestion was "personList" not "PersonList" and since a class never should begin with a Capital letter I have no problem with "personList". But I would have gone with calling it "persons".
-
Михаил Ммк about 4 years10 years ago! It was probably a typo. Tbh, I couldn't care less these days.