gpt4 book ai didi

c# - 从月份数组中获取一系列值

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:04:13 26 4
gpt4 key购买 nike

我创建了一个数组 [Framework version 2.0, C# 2.0] 来存储一年中的月份

来源

public readonly static string[] Months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

我正在寻找一种方法来检索 IEnumerable返回 range of months从这个静态列表。我可以想出很多方法,但我来这里是为了找到一个让我走的方法 wahhhhhh...该方法的签名看起来像

签名

public IEnumerable<String> GetRange(int startIndex,int endIndex);

示例 I/O

startindex = 1
endindex = 10
returns months from January ,February,March upto October

注意: Array.Copy很整洁,但使用参数的方式变得古怪

Parameters

sourceArray

The Array that contains the data to copy.

sourceIndex

A 32-bit integer that represents the index in the sourceArray at which copying begins.

destinationArray

The Array that receives the data.

destinationIndex

A 32-bit integer that represents the index in the destinationArray at which storing begins.

length

A 32-bit integer that represents the number of elements to copy.

最佳答案

如果你想使用 Array.Copy 你可以这样做:

public IEnumerable<String> GetRange(int startIndex, int endIndex)
{
int numToCopy = endIndex - startIndex + 1;
string[] result = new string[numToCopy];
Array.Copy(Months, startIndex - 1, result, 0, numToCopy); // startIndex - 1 because Array indexes are 0-based, and you want the first element to be indexed with 1
return result;
}

这适用于 .NET 2.0

关于c# - 从月份数组中获取一系列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7979002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com