Subset of Array in C#

后端 未结 8 1236
别跟我提以往
别跟我提以往 2020-12-05 16:49

If I have an array with 12 elements and I want a new array with that drops the first and 12th elements. For example, if my array looks like this:

__ __ __ _         


        
8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 17:31

    C# 8 has a Range and Index type

    char[] a = { 'a', 'b', 'c',  'd',  'e',  'f',  'g',  'h',  'i',  'j',  'k',  'l' };
    Index i1 = 1;  // number 1 from beginning
    Index i2 = ^1; // number 1 from end
    var slice = a[i1..i2]; // { 'b','c','d','e','f','g','h','i','j' }
    

提交回复
热议问题