C#.Net array naming conventions with examples

C# is a statically-typed language, which means variables must be declared before they’re used. One of the most useful features of C#.Net is its robust support for arrays, which allow you to store multiple values in a single variable. In this tutorial, we’ll discuss the naming conventions for arrays in C#.Net with some real examples.

Below is the array naming conventions in C#.

Array TypeExample NameCode Example
Single-Dimensionalcitiesstring[] cities = { "NewYork", "LosAngeles", "Chicago", "Houston", "Phoenix" };
Multi-DimensionalcityPairsstring[,] cityPairs = { { "NewYork", "LosAngeles" }, { "Chicago", "Houston" }, { "Phoenix", "Seattle" } };
Array of ArrayscityGroupsstring[][] cityGroups = { new string[] { "NewYork", "LosAngeles", "Chicago" }, new string[] { "Houston", "Phoenix", "SanFrancisco" }, new string[] { "Seattle", "Austin", "Jacksonville" } };

A naming convention is a set of rules for choosing the character sequence to be used for identifiers which denote variables, types, functions, and other entities in source code and documentation.

In C#, the commonly followed naming conventions are:

  1. Camel Case (camelCase): The first letter of an identifier starts with a lowercase letter and the first letter of each subsequent concatenated word starts with an uppercase letter.
  2. Pascal Case (PascalCase): The first letter of an identifier and the first letter of each subsequent concatenated word starts with an uppercase letter.

General Guidelines for C#.Net array naming conventions

Here are some general guidelines to keep in mind when naming arrays:

  • Descriptive Names: Names should be descriptive and represent what the array contains.
  • Use Plural Form: Since arrays hold multiple values, names should be in plural form.
  • Avoid Using Array In The Name: Do not use the word “array” in the name.
  • Case Style: For local variables and parameters, camelCase is used, while for properties, PascalCase is used.

C#.Net Array Naming Conventions Examples

Let’s consider a few examples of C#.net array with proper naming conventions.

Example 1: Single-Dimensional Array

string[] cities = { "NewYork", "LosAngeles", "Chicago", "Houston", "Phoenix" };

Here, cities is a good name because it’s plural (indicating an array) and it represents what the array contains.

Example 2: Multi-Dimensional Array

string[,] cityPairs = {
    { "NewYork", "LosAngeles" },
    { "Chicago", "Houston" },
    { "Phoenix", "Seattle" }
};

Here, cityPairs is a good name because it’s plural and it represents what the array contains (pairs of cities).

Example 3: Array of Arrays (Jagged Arrays)

string[][] cityGroups = {
    new string[] { "NewYork", "LosAngeles", "Chicago" },
    new string[] { "Houston", "Phoenix", "SanFrancisco" },
    new string[] { "Seattle", "Austin", "Jacksonville" }
};

Here, cityGroups is a good name because it’s plural and it represents what the array contains (groups of cities).

c#.net string array naming conventions

In C#.NET, the naming conventions for string arrays is largely similar to the general array naming conventions in C# which were discussed earlier. Here are the general guidelines:

  • The names should be descriptive and represent what the array contains.
  • Since arrays hold multiple values, names should be in plural form.
  • Avoid using the word “Array” in the name.
  • For local variables and parameters, use camelCase.
  • For properties, use PascalCase.

Let’s look at some specific examples of string array naming conventions in C#.NET.

Example 1: Single-Dimensional String Array

string[] cityNames = { "New York", "Los Angeles", "Chicago", "Houston", "Phoenix" };

Here, cityNames is a good name because it’s plural (indicating an array) and it represents what the array contains – names of cities.

Example 2: Multi-Dimensional String Array

string[,] stateAndCapital = {
    { "California", "Sacramento" },
    { "Texas", "Austin" },
    { "New York", "Albany" }
};

Here, stateAndCapital is a good name because it’s plural and it represents what the array contains – pairs of states and their capitals.

Example 3: Jagged String Array

string[][] countryCityGroups = {
    new string[] { "USA", "New York", "Los Angeles", "Chicago" },
    new string[] { "UK", "London", "Birmingham", "Glasgow" },
    new string[] { "France", "Paris", "Marseille", "Lyon" }
};

Here, countryCityGroups is a good name because it’s plural and it represents what the array contains – groups of cities under each country.

C#.Net array naming conventions
C#.Net array naming conventions

Conclusion

The following are the key points to remember when naming arrays in C#:

  • Use descriptive names.
  • Use the plural form.
  • Avoid using “Array” in the name.
  • For local variables and parameters, use camelCase.
  • For properties, use PascalCase.

You may also like: