Selecting a range of items inside an array in C#

后端 未结 1 729
庸人自扰
庸人自扰 2020-12-16 09:18

I would like to select a range of items in an array of items. For example I have an array of 1000 items, and i would like to \"extract\" items 100 to 200 and put them in ano

相关标签:
1条回答
  • 2020-12-16 09:46

    In C# 8, range operators allow:

    var dest = source[100..200];
    

    (and a range of other options for open-ended, counted from the end, etc)

    Before that, LINQ allows:

    var dest = source.Skip(100).Take(100).ToArray();
    

    or manually:

    var dest = new MyType[100];
    Array.Copy(source, 100, dest, 0, 100);
           // source,source-index,dest,dest-index,count
    
    0 讨论(0)
提交回复
热议问题