Push to arrays #647
Replies: 11 comments
-
Sorry pushed enter on my phone too quickly int data = new int [1,2,3] So that data is now 1,2,3,4 |
Beta Was this translation helpful? Give feedback.
-
Considering the difference between arrays in C# and "arrays" in PHP I don't think that it's appropriate to enshrine such an operation in a special language operator. Appending to an array is quite expensive and shouldn't be encouraged. If it's something that you really want to do you can already define an extension method that would handle creation of a new array, appending the item(s) and returning the new value: public static class ArrayExtensions {
public static T[] Append<T>(this T[] array, T item) {
if (array == null) throw new ArgumentNullException(nameof(array));
T[] copy = new T[array.Length + 1];
Array.Copy(array, copy, array.Length);
copy[array.Length] = item;
return copy;
}
} |
Beta Was this translation helpful? Give feedback.
-
@Rottweiler Arrays have fixed-size semantics everywhere they are used. People's code takes dependencies on the fact that nothing can cause them to resize. |
Beta Was this translation helpful? Give feedback.
-
This is a weird syntax even for PHP. |
Beta Was this translation helpful? Give feedback.
-
I could maybe see doing something similar to this if there were a C# language mechanism for defining a var data = new list { 1, 2, 3 }; // equivalent to new List<int> { 1, 2, 3 }
data += 4; // appends 4 EDIT: Actually I guess that doesn't matter much, |
Beta Was this translation helpful? Give feedback.
-
There is much about arrays (i.e. fixed size, compiler optimizations, primitiveness) that make it unlikely their further development. But for collections (lists, dictionaries), such a feature would be nice. |
Beta Was this translation helpful? Give feedback.
-
@bondsbw |
Beta Was this translation helpful? Give feedback.
-
There's almost no chance we would provide this. However this may fall out if we ever allow extension operators. Then you could just define your own |
Beta Was this translation helpful? Give feedback.
-
Also if we get object initializer syntax on already-created objects, that would look very similar. |
Beta Was this translation helpful? Give feedback.
-
@jnm2 Soooo.... Essentially the "port VB's |
Beta Was this translation helpful? Give feedback.
-
@Joe4evr It's around here somewhere. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions