7

I am basically c++ guy,now learning c#. While array declaration I found that c# expects the square brackets after the type unlike c,c++. Is there any reason c# language specification is this way?

4
  • 5
    I am also a C++ guy, but I must say I find the C array declaration syntax counter-intuitive. May 7, 2013 at 7:11
  • @juanchopanza ya,i agree,but now used it :)
    – ZoomIn
    May 7, 2013 at 9:45
  • In fact the actual type in C++ is also T[N] (with the brackets directly after the base type), it's just the declaration that has to be written differently, which is indeed a bit contradictory. May 7, 2013 at 12:41
  • Does c++ array size can be changed later after declaration ?
    – user991513
    Sep 30, 2016 at 18:56

2 Answers 2

5

The only good reason I can think of is to make a clear indication of the types of list1 and list2 in this construction:

In C#:

int[] list1, list2;

list2 is an array of ints.

In C++:

int list1[], list2;

list2 is an int.

Readability is key here.

1
  • That doesn't compile in C++ though - you need "int list1[], list2". May 7, 2013 at 14:21
3

It's also for consistency (which helps readability).

In a C# variable declaration, the complete type specification is always on the left of the variable name.

In C/C++ sometimes part of the type specification is on the right of the variable name, which is inconsistent.

2
  • Can you give me an example of part of type specification on the right side of =, in C++
    – ZoomIn
    May 7, 2013 at 8:54
  • @ZoomIn Sorry, I meant to say "variable name", not "="... I've corrected my post. May 7, 2013 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.