-2

create an array is almost most common and easy task in programming, but after I dive deeper in C#, I found there is sth more than intuitive mind. We all know to create an array, we can:

int[] myInts = new int[3];
myInts[0] = 1;
myInts[1] = 2;
myInts[2] = 3;

int[] myAnotherInts = new int[] { 1, 2, 3,};  // Array Initialization Syntax

But we can't do like:

int[] myInts = new int[3](); // why we can't add () in the end?

while we do use () on collection as:

List<int> myInts = new List<int>() { 1, 2, 3 };

Question 1:

why we can't add () in the end?

Question 2:

if the answer to my question 1 is Array is an abstract class as:

public abstract class Array : ICloneable, IList, ICollection, IEnumerable...

but the 'new' keyword is also used, which means that a constructor is called, which means that an instance is created, isn't it against the abstract class rule that no instance is allowed to be created?

  • 1
    Psst: try `List myInts = new List { 1, 2, 3 };` – ProgrammingLlama Jun 03 '19 at 00:36
  • 4
    `// why we can't add () in the end?` Because the language syntax says you can't. There isn't really a better answer than that. – mjwills Jun 03 '19 at 00:37
  • 1
    https://mattwarren.org/2017/05/08/Arrays-and-the-CLR-a-Very-Special-Relationship/ and https://stackoverflow.com/questions/19914523/mystery-behind-system-array#comment29631862_19914523 may be worth a read. – mjwills Jun 03 '19 at 00:37

1 Answers1

0

why we can't add () in the end?

What are you hoping it will do? C based languages aspire to be terse; you can see with introduction of the array initialisation syntax you referenced that the c# dev team are interested in providing ways to make code more compact and simple while still being readable. Consider LINQ vs looping, string interpolation, the enhancement of “is” to provide an instance, shorthand properties, the async/await mechanism, the null propagator and ?? operator.. all avoidable shorthands for something we used to write out more fully

Empty brackets after an array dimensioning statement don’t add anything helpful, readable, simplified, nor do anything practical, they just increase the number of keys pressed when an array is declared. In the context of a normal constructor however they do add something useful- they tell the compiler and your peers “I intend to invoke the parameterless constructor”. You don’t get this choice of constructor when making an array (eg an int[] that derives from Array), so it doesn’t make sense to provide a facility for it. You also need to remember that a dimensioned array is typically full of nothing; you can make an array of a million strings but only one constructor has been called; the one to make the Array.. the array slots are null, waiting for you to say array[x] = new String(...)

If you aim to promote the notion that providing brackets or bracketed values means we could not only declared an array but initialise every element in the array with a new object whose constructor is called with the passed bracketed arguments then sure, its an option and you could suggest it to the language team; I feel it wouldn’t gain much traction because an array that is full of identical objects is probably a narrow use case at best and could be mostly addressed by subclassing and declaring a custom default value for your class

the 'new' keyword is also used, which means that a constructor is called, which means that an instance is created, isn't it against the abstract class rule that no instance is allowed to be created?

I think I understand what you’re saying but you have to appreciate that Array is a base class for all arrays, you do not instatiate it directly yourself so no rules of abstraction are being broken. You make an int[] and internally the compiler/system do the shuffling necessary to have it derive from Array; you’re generally advised to keep to using the provided language constructs to create things such as ibt[] that concretely derive from the abstract Array. If a constructor is called you don’t get to see it, or provide arguments to it so there isn’t really a need to demand that you write brackets-anything

One thing to bear in mind is that the compiler does a huge amount of rewriting the code you write before it even compiles it down to IL. Most of the shorthands listed above will be silently expanded to long hand variations, sometimes of considerable complexity, by the compiler so if you’re reading something and think “that doesn’t look like c# 1.0” just know that it orbabky looks a lot more like the original longest-hand form of c# by the time the compiler is done preprocessing it

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • so when you mentioned int[] that derives from Array, could you show me the type of derived type int[]? I want to see it in the VS's Go to definition (F12) to see how it derives from Array base class –  Jun 03 '19 at 08:02