-2
string[] Names = {};
string[] names = {};

Is it better to use a capital letter? Is convention the same for arrays and other types?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
jskidd3
  • 4,609
  • 15
  • 63
  • 127
  • 4
    -4? Oops, why is this question poor? – jskidd3 Aug 06 '13 at 16:05
  • In general, all **variables** should have a `lowercaseCamelCase` name. – Jonathon Reinhart Aug 06 '13 at 16:05
  • You will learn any programming languages better by testing things like this yourself. – Dour High Arch Aug 06 '13 at 16:06
  • The first question could be answered by trying to compile. – Austin Salonen Aug 06 '13 at 16:06
  • Style-wise, it is customary in C# to have variable names beginning in lowercase and property names beginning in uppercase. Doesn't matter if it is an array or not. – Jan Dörrenhaus Aug 06 '13 at 16:06
  • I didn't say that though, sure I could test myself. I'm asking which ones are better suited to C# – jskidd3 Aug 06 '13 at 16:06
  • [C# Coding Conventions](http://msdn.microsoft.com/en-us/library/vstudio/ff926074.aspx) –  Aug 06 '13 at 16:07
  • @JanDoerrenhaus, I think you mean UpperCamelCase and lowerCamelCase? – Jordy Aug 06 '13 at 16:07
  • It depends on your coding guidelines. You can get a good example at http://www.idesign.net/Downloads/GetDownload/1985 – Lukas Winzenried Aug 06 '13 at 16:08
  • Thanks for the help. Since when are questions that can be put into a compiler not valid for Stack Overflow, though? – jskidd3 Aug 06 '13 at 16:09
  • 5
    Since point 1 of this list: http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist – Jan Dörrenhaus Aug 06 '13 at 16:10
  • Sure, difference is I did compile. Perhaps asking if they were invalid or not shouldn't have been part of my question. The main thing I was getting at was capitalization and the compiler won't tell you about that. – jskidd3 Aug 06 '13 at 16:11
  • :) and what what result of compiling `string names[] = {};` ? There is no way to see if you did work on your question of simply throw random text if it looks like random text... – Alexei Levenkov Aug 06 '13 at 16:16
  • 4
    @JoelKidd In addition to Jan's point, it's [even in the help center](http://meta.stackoverflow.com/help/how-to-ask) as the *very first* point on how to ask a question. – Servy Aug 06 '13 at 16:18
  • Ok, probably best the close/delete the question altogether then. Thanks to people that assisted me here though, helped me better my understanding a lot :) – jskidd3 Aug 06 '13 at 16:21
  • I've completely changed your question to hopefully match what you wanted to ask. Feel free to revert... – Alexei Levenkov Aug 06 '13 at 16:27
  • @AlexeiLevenkov Thanks Alexei. Your edited question is probably better should anyone find it in the future. – jskidd3 Aug 06 '13 at 16:39
  • 1
    Voting to re-open (or rather keep from deleting :) as it have good answers (also after removing unrelated invalid syntax it is more acceptable). – Alexei Levenkov Aug 06 '13 at 17:10
  • @JoelKidd I do this: I use [StyleCop](http://stylecop.codeplex.com). If it eats the code without complaining then the formatting is good enough :-) – xanatos Aug 06 '13 at 17:17

2 Answers2

5

You'll notice in C# examples and .NET framework naming, that in general, everything is camelCase:

  • All variables should have a lowercaseCamelCase name.
  • All types (class, struct, enum, delegate), methods, and properties should have an UppercaseCamelCase (aka PascalCase) name.

Here is an example:

// Types are CamelCase
class Foo {

    // Properies are PascalCase
    public int SomeProperty { get; set; }

    // *Private* fields are lowerCamelCase
    private int aField;

    // By some conventions (I use these)
    private int m_anotherField;            // "m_" for "member"
    private static object s_staticLock;    // "s_" for "static"

    // *Public* fields are PascalCase
    public int DontUsePublicFields;        // In general, don't use these
    public const int ConstantNumber = 42;

    // Methods are UpperCase
    // Parameters and variables are lowerCase
    public SomeMethod(int myParameter) {
        int[] localVariable;

        //string names[] = {};     // Not valid C#! 
        string[] names = new string[] { "Jack", "Jill" };
    }
}

See also:

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Can you define a "type"? – jskidd3 Aug 06 '13 at 16:08
  • 1
    `UpperCamelCase` == `PascalCase` in the documentation. It's not just types, either - fields and properties are supposed to use PascalCase. – Reed Copsey Aug 06 '13 at 16:08
  • 3
    @JoelKidd He does make a valid point, though. You'll find on StackOverflow that we, as a community, place a **heavy** importance on doing your own research, and basically seek to use SO as "*last chance, couldn't find it anywhere else on the internet, so let's figure it out and* **put it on the internet**" kind of resource. That being said, it was simple enough for me to expand on my use of the word "type". – Jonathon Reinhart Aug 06 '13 at 16:20
  • 1
    @JonathonReinhart He does indeed, however the way you've just put your point across was much more pleasant to read than tnw's. From now on I will use SO as a last resort. Thanks for telling me this. – jskidd3 Aug 06 '13 at 16:22
  • Do you use singular or plural variable names? https://stackoverflow.com/questions/395739/do-you-name-your-arrays-plurally-or-singularly – Ola Ström Mar 23 '20 at 14:18
1

Are all of those valid? Is it better to use a capital letter?

The last (string names[] = {};) is not valid C#. The other two are correct in terms of syntax.

The suggested capitalization for a variable depends on the usage. Local variables or parameters to a method typically would be named with a lower case letter (technically, camelCase), fields properties would be upper case (technically, PascalCase). For details, see the Capitalization Conventions.

Note that these conventions have nothing to do with the type of variable, but rather it's usage. The fact that you're dealing with an array shouldn't necessarily change the variable's name - at least not the capitalization of the variable name. (You may, however, want to use a name that suggests that the variable is a "collection" of some form.)

That being said, this is purely a convention, and it is entirely up to you what naming you use.

If it is better to use a capital letter, does this mean that the arrays are treated as objects?

Capitalization has nothing to do with how variables are treated - only their types. Changing the capitalization may suggest to other developers that the type is a field or property, but it does not actually change the variable in any way from the language or usage perspective.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373