Subset of Array in C#

后端 未结 8 1237
别跟我提以往
别跟我提以往 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:32

    You can do this with Array.Copy or LINQ.

    var letters = string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i" };
    
    int length = letters.Length - 2;
    var items = new string[length];
    Array.Copy(letters, 1, items, 0, length);
    // or
    var items = letters.Skip(1).Take(length).ToArray();
    

提交回复
热议问题