Proposal: Array Allocation and Initialization with [..] and Range-Value Fill Syntax #9527
Replies: 5 comments 6 replies
-
|
Beta Was this translation helpful? Give feedback.
-
There are plans to change that by allowing collection expressions to have natural types. |
Beta Was this translation helpful? Give feedback.
-
Terse != readable. The examples are very ambiguous to my human eyes as to what they mean. |
Beta Was this translation helpful? Give feedback.
-
I think that this existing discussion is relevant. even though the syntax is different: #9515 |
Beta Was this translation helpful? Give feedback.
-
Closing as dupe of #9515. Let's keep the discussion and syntax ideas there. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Introduce concise syntax for array allocation and initialization using range literals, inspired by collection expressions and slicing:
This allows more expressive, readable, and compact array declarations and eliminates boilerplate when working with large or pattern-based arrays.
Motivation
C# has evolved toward more concise, expressive syntax (e.g.,
new()
, collection expressions, ranges). However, array allocation remains verbose, especially when initializing large or structured arrays:This proposal introduces a syntax that is:
Easy to read and write
Familiar to anyone using ranges or slicing
Compatible with type inference on the left-hand side
Extensible to future features (spans, multidimensional arrays)
Syntax Details
✅ Basic Allocation
✅ Uniform Initialization
✅ Range-Value Initialization
Fills
arr[0..63]
with2
andarr[64..127]
with3
.✅ Size Inference from Ranges
The compiler determines the array size from the highest index used.
✅ Mixed
Parsing & Disambiguation
[..N]
without a target is not currently valid, so using it in a declaration is unambiguous.In a declaration like
int[] arr = [..128];
, the compiler knows the expected type (int[]
) and can treat[..128]
as array allocation, not a Range.If
[..N:V]
orstart..end:V
expressions are used, the compiler:Grammar (Conceptual)
Lowering Semantics
These expressions would lower to loops and Array.Fill calls, e.g.:
Becomes:
Compatibility
✅ No conflicts with existing syntax:
[..N]
is currently legal but meaningless alone (only usable in slicing)[..N]
in assignment without an index target✅ Backwards compatible
❌ Not allowed with
var
:Benefits
✅ Eliminates boilerplate for array allocation and initialization
✅ Improves readability and alignment with modern C# features
✅ Efficient: compiler can emit optimized loops or even static data
✅ Type-safe and bounds-checked
✅ Easy to extend to spans, multidimensional arrays, or other memory abstractions in the future
Future Directions
This syntax could potentially support:
Span<T>
:T[,]
multidimensional arrays (e.g.,[0..3, 0..3:1]
)Conclusion
This proposal provides a clean, expressive way to allocate and initialize arrays with syntax that is readable, compact, and aligned with C#'s design direction. It enhances the language's ability to concisely describe data layouts without sacrificing clarity or performance.
Beta Was this translation helpful? Give feedback.
All reactions