-1

I know that the following C# code will not compile:

int[10] TestFixedArrayReturn(int n)
{
   return new int[10]{n, n, n, n, n, n, n, n, n, n};
}
void TestCall()
{
   int[10] result = TestFixedArrayReturn(1);
}

In order to get it to compile, I need to remove the array size from the function declaration (as well as the declaration of the result variable) like so:

int[] TestFixedArrayReturn(int n)
{
   return new int[10]{n, n, n, n, n, n, n, n, n, n};
}
void TestCall()
{
   int[] result = TestFixedArrayReturn(1);
}

I'm just wondering--why is that I cannot specify the size of the array of ints which will get returned? I take it what's getting passed back is actually a reference to the array (that'd be my guess anyway) but why can't I specify the size of the array being returned? Wouldn't this allow the compiler to check my code more closely for correctness?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132
  • 4
    Because the size of an array is only specified during initialization. – Rufus L Dec 16 '19 at 17:30
  • 3
    Don’t know why people are downvoting this, it’s a perfectly reasonable question. – RBarryYoung Dec 16 '19 at 17:47
  • @RufusL Doesn't an array have to be initialized to be returned from a function? I guess I'm not understanding your comment? – Onorio Catenacci Dec 16 '19 at 17:51
  • 1
    How would you differentiate a method that should return an array of size 10 vs 20? The types are not different and the size doesn't play into the method signature. I guess I don't totally see what doing this would buy you. And if you needed to change the size, you now need to change it in 3+ places (potentially in different projects that you'd need to track down). – Broots Waymb Dec 16 '19 at 17:52
  • 2
    @OnorioCatenacci The function’s return value/object is not initialized declaratively in the function declaration, but procedurally in the function itself. – RBarryYoung Dec 16 '19 at 17:54
  • @OnorioCatenacci *"Doesn't an array have to be initialized to be returned from a function?"* Yes, it must be initialized (even if only to `null`), before it can be returned. That is the when you specify the size. *"I'm not understanding your comment?"* My comment is that the return **type** (specified in the method signature) is not where initialization takes place, so it can't be specified there. – Rufus L Dec 16 '19 at 18:02
  • 1
    That battle was fought and won a very long time ago. The creator of the C# language sat on first row. Kernighan's 1981 paper [is still available](http://www.lysator.liu.se/c/bwk-on-pascal.html), chapter 2.1. Somewhat misleading perhaps, the CLR design does dictate what a language can do in a portable way. – Hans Passant Dec 16 '19 at 18:20
  • That's the sort of answer I was actually seeking @HansPassant! Thanks for that pointer! – Onorio Catenacci Dec 16 '19 at 20:37

3 Answers3

3

An array is an array... it doesnt have a size unless you initialize it. When you are passing a variable from a method to another method, you define the type not its structure as in the size of the array.

Jawad
  • 11,028
  • 3
  • 24
  • 37
3

The simple answer I think, is that in .net:

  1. Functions return instances of a type
  2. Therefore, functions are declared as having a return type
  3. Arrays are a single type (Class) in .net, so that they can interoperate
  4. Conversely, however, this means that arrays of different fixed sizes do not have different Types, just different instance attributes.
  5. Which means that functions cannot be defined to return arrays of a specific fixed size because the length of an array is not part of its type specification, but rather part of its instance settings/attributes.

For variables, this seems like it can be done in the type declaration of variables, but it is actually part of the instance initialization specs there, even though syntactically it looks like it’s part of the type declaration. This was probably retained for style compatibility reasons with older languages like C, where things worked much differently under the hood.

For functions, however, the return object is not initialized where it is declared (in the function declaration) but rather procedurally in the body of the function itself. So allowing instance attributes to be set in the function declaration could have caused all kinds of conflicts and edge cases that would need to be checked either by the compiler or at run-time, neither of which was probably seen as being worth the very minimal gain.

RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
1

How would the compiler know at compile time what the size of the array being returned would be? To work this out it would have to execute all the code in your method...

Milney
  • 6,253
  • 2
  • 19
  • 33
  • "it would have to execute all the code in your method" - which could also depends on outside resources for that matter. What if the elements are fetched from a web service call or a database? This seems like a super strange thing to want to do. – Broots Waymb Dec 16 '19 at 17:34