It's impossible to guarantee that count and elementat have no side effects unless you fully devirtualize everything, which can't be done at compile time since the application's dependencies (SDK/runtime, third party libraries) could be swapped out before it runs.
So these optimizations would have to occur in the JIT and might come at the cost of worse startup time or memory usage.
Fwiw modern .net is getting pretty good at devirt but I don't expect it would optimize all this out.
So these optimizations would have to occur in the JIT and might come at the cost of worse startup time or memory usage.
Fwiw modern .net is getting pretty good at devirt but I don't expect it would optimize all this out.