'Index from End' operator generates error?

The ‘Index from End’ operator ^x was introduced in C#8 as syntactic sugar for numbers.Length - x.

Intellisense suggested the use of ‘Index from End’ operator for my small test program. I tested the same code in the Fire IDE (Hydrogene) and it unfortunately generates an ‘NullReferenceException’ error. But the same exact code does NOT generate an error in the Water IDE. I’m not sure why … but thanks for looking! :cowboy_hat_face:

Build_log1.txt (2.5 KB)

OK, I think I figured out partially why this is occurring. When I run the programs using top-level statements - the errors occur in both Fire and Water IDE’s. So it appears the partial ‘solution’ is NOT to use top-level statements:

That definitely looks like a bug, even more so if it only fails on top level. can I see a simple test case for this? What is the platform?

1 Like

Here’s a simple test case, I’ve tested it out with Fire IDE (2903) running various versions of .Net (from .NetCore to .Net8.0). This version work as is. Just comment out the boiler template code to see the error described above.

namespace IndexFromEnd_CS
    static class Program
        public static Int32 Main(string[] args)
            var numbers = new int[]{2, 6, 1, 6, 19};
            //var firstFromEnd = numbers[numbers.Length - 1];
            var firstFromEnd = numbers[^1];

            //var secondFromEnd = numbers[numbers.Length - 2];
            var secondFromEnd = numbers[^2];


            return 0;

I get a different error (IE) if I change it (ofc that will also need to be fixed, I will log a bug).

But can you please send me a project that reproduces the exact error you have unchanged, so I doing have to guess what changes to make? thanx! :pray:t3:

1 Like

Logged as bugs://E26890: C#: compiler NRE with “top-level” code for main.

Hey Marc,
Here’s a OneDrive link for the Water project in question:

And here’s the Fire project from my MBP which uses Dropbox instead.


1 Like

bugs://E26890 was closed as fixed.

1 Like