Proposal to Extend C# Array Indexing for Relative Access #70499
Replies: 5 comments 1 reply
-
In addition to the previously proposed relative array indexing using the Proposed Syntax:
Where Example:int[] values = new int[9];
Console.WriteLine(values[^/3]); // This would be equivalent to values[6] |
Beta Was this translation helpful? Give feedback.
-
I don't understand the use cases. When would you want to do this? And what makes you think those use cases are common enough to justify a dedicated language syntax? Especially considering that you can very easily write an extension method that does the same thing, if it's a common operation in your domain. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the feedback! I understand your reservations. Here's a brief rundown: Readability:
Error Prevention:This syntax inherently reduces common off-by-one errors. Consistency:C# introduced the Performance:Native syntax can be optimized at the compiler level, potentially outpacing extension methods. Discoverability:Language features are more easily discovered and adopted than library-specific methods. This has potential use cases in several fields. Example Working with Date Data analytics:Suppose you have an array representing stock prices recorded every day for a month. double[] stockPrices = GetMonthlyStockPrices();
double midMonthPrice = stockPrices[/2]; //get the price in the middle of the month
double QuarterlyAvgPrice = (stockPrices[/4] + stockPrices[/2] + stockPrices[^/4] + stockPrices[^]) / 4; Image Manipulation:Imagine cropping an image to remove the outer thirds:
This immediately showcases a central cropped region, making the code expressive and self-documenting. This syntax would be especially handy in domains like image processing, graphics, and gaming where dynamic cropping is frequent. Let me know your thoughts. |
Beta Was this translation helpful? Give feedback.
-
Ranges aren't supported by multi dimensional array which is a problem in the texture example. Currently from what I know which would be nice cuz It could be used with matrix arrays you can pull the underlying matrix like pulling a 3x3 from a 4x4. From what I know data analytics is used in almost every field like for performance analytics and stuff and I've personally had to use it cuz when you come to monitoring 1% lows in game I was keeping track of the last 100 frame times would have another array as the sorted version and would need to know what the 10th section is of the sorted one or 100th section I do understand that this is not a completely accurate representation of 1% but in game dev it provides I quick way to put a number to lag spikes |
Beta Was this translation helpful? Give feedback.
-
I don't understand how it's readable. I can't map What will be the result if Length isn't divisible by n? How to represent the positions at 2/n, 3/n, 4/n? 1/n isn't special. |
Beta Was this translation helpful? Give feedback.
-
Overview:
This proposal suggests adding a new indexing syntax to C# arrays that allows developers to access array elements relative to the array's length. The primary aim is to enhance readability and reduce common off-by-one errors, especially when working with dynamic arrays whose length might change.
Syntax:
The proposed syntax introduces the
/
symbol before a number inside the array index, which will act as a shorthand to access elements relative to the array's length.Where
n
is the nth part of the array's length.Examples:
Benefits:
length/2 - 1
. This syntax would help reduce such errors.Potential Issues and Concerns:
Implementation Details:
The C# compiler will be responsible for translating this new syntax into the standard array indexing. It will calculate the actual index at compile-time (for static lengths) or runtime (for dynamic lengths) and replace the new syntax with standard indexing.
For example:
Will be translated to:
Conclusion:
By introducing this new array indexing syntax, C# can offer a more expressive and error-reducing way to access array elements relative to its length. This is just a proposal that aims to gather feedback and iterate on any concerns or suggestions.
Beta Was this translation helpful? Give feedback.
All reactions