Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable naming for arrays/lists/collections - C# [closed]

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 
}
like image 432
David Neale Avatar asked May 06 '10 07:05

David Neale


People also ask

What is the naming convention for variables in C?

Rules for naming a variableA variable name can only have letters (both uppercase and lowercase letters), digits and underscore. The first letter of a variable should be either a letter or an underscore. There is no rule on how long a variable name (identifier) can be.

What are the 4 rules for variable names?

A variable name must start with a letter or an underscore character (_) A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)

What is a good naming convention for variables?

Variables of general types (primitive and composite data types) should be named logically. Variables of specialized types (extended data types) should have the same name as the type (which should have a logical name) but starting with a lower case character.


2 Answers

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.

like image 87
thelost Avatar answered Sep 28 '22 06:09

thelost


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.

like image 41
Johan Badenhorst Avatar answered Sep 28 '22 07:09

Johan Badenhorst