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. Commented May 7, 2013 at 7:11
  • @juanchopanza ya,i agree,but now used it :) Commented 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. Commented May 7, 2013 at 12:41
  • Does c++ array size can be changed later after declaration ? Commented 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.

Sign up to request clarification or add additional context in comments.

1 Comment

That doesn't compile in C++ though - you need "int list1[], list2".
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 Comments

Can you give me an example of part of type specification on the right side of =, in C++
@ZoomIn Sorry, I meant to say "variable name", not "="... I've corrected my post.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.