-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Comments
Adding redundant size again when you have an initializer? The existing form seems easier to me:
What you currently can do is all not much longer than what you propose:
I could imagine where this could make sense when it's embedded in a struct, something like an inlined array. |
@FredyFerrari object[] o = { null, new object() }; //create a new array object on the heap, the local variable "o" only stores the array's reference. |
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? |
The syntax alone is enough to warrant a downvote for me. In many languages 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. |
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. |
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
}
}
The text was updated successfully, but these errors were encountered: