Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Allow fixed size array to be static field and local variable #212

Closed
ygc369 opened this issue Mar 1, 2017 · 5 comments
Closed

Comments

@ygc369
Copy link

ygc369 commented Mar 1, 2017

For example:
class A
{
static int b[3]{0,1,2}; //static field with initialization
static object c[10]; //static field without initialization, set to default
......
void func()
{
object o[2]{null, new object()}; //local variable with initialization
int a[3]; //local variable without initialization, set to default
}
}

@FredyFerrari
Copy link

FredyFerrari commented Mar 1, 2017

Adding redundant size again when you have an initializer?

The existing form seems easier to me:

static int[] b = { 0, 1, 2 };

What you currently can do is all not much longer than what you propose:

        class A
        {
            static int[] b = { 0, 1, 2 };
            static object[] c = new object[10];
        }

        void func()
        {
            object[] o = { null, new object() };
            var a = new int[3];
        }

I could imagine where this could make sense when it's embedded in a struct, something like an inlined array.

@ygc369
Copy link
Author

ygc369 commented Mar 1, 2017

@FredyFerrari
What I really want to change is not the syntax, but the location to allocate memory.
The differences are:
static int[] b={0,1,2}; //create a new array object on the heap, the static field "b" only stores the array's reference.
static int b[]{0,1,2}; //the "array" itself is stored in the same region as other static fields.

object[] o = { null, new object() }; //create a new array object on the heap, the local variable "o" only stores the array's reference.
object o[] { null, new object() }; //the "array" itself is stored on the stack, not on the heap.

@FredyFerrari
Copy link

That's what I meant with: I could imagine where this could make sense when it's embedded in a struct, something like an inlined array.

But then, wouldn't it make more sense to define the array as something like inline? Instead of having a different syntax which is not intuitive?

@HaloFour
Copy link
Contributor

HaloFour commented Mar 1, 2017

The syntax alone is enough to warrant a downvote for me. In many languages int[] a and int a[] are equivalent. C# eliminated the second form to force consistency; the type is always on the left side of the declaration, not partially on the left and partially on the right.

As for the functionality, I'm not entirely sure what you intend to do with these "pseudo-arrays"? I understand that you want to allocate them on the stack rather than on the heap, but how do you intend to work with them? You can allocate a block on the stack and treat it like an array using unsafe code today in C#. I don't believe that you could do so in a safe manner, though, since there is no bounds checking.

@fanoI
Copy link

fanoI commented Mar 1, 2017

The correct way to do this is using "stack escape analysis" a thing that should be done not at C# level but at CLR / Jitter level, ideally a lot of thing (classes too in particular if immutable one as String) should be allocated onto the stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants