1

The following code compiles:

var ints = Enumerable.Range(0, 10).ToArray();
var subset = ints[2..^2];

But this code doesn't:

var ints = Enumerable.Range(0, 10).ToList();
var subset = ints[2..^2];

error message

The error message is, as you can see from the picture above, cannot convert from 'System.Range' to 'int'.

Why is it that the C# 8 range operator only seems to work with arrays and not with lists? How do I use it with a list?

Daniel Jonsson
  • 3,261
  • 5
  • 45
  • 66
  • You should be able to use linq 'Take' instead i.e: ```var subset = ints.Take(2..^2);``` See https://dotnetfiddle.net/To2sYo – Adam Stawarek Mar 14 '22 at 14:14

0 Answers0