C# 如何从数组或列表中选择值<;类型>;在低索引和高索引值之间?

C# 如何从数组或列表中选择值<;类型>;在低索引和高索引值之间?,c#,arrays,list,C#,Arrays,List,我有两个集合变量,如下所示: public static int[] intArray = {0,1,2,3,4,5,6,7,8,9,10 }; public static List<int> intList = new List<int>(); //The list also has same elements as the array. 注意:这同样适用于intList。可以通过.ToList()方法将intArray解析为列表。列表有一个名为GetRange(in

我有两个集合变量,如下所示:

public static int[] intArray = {0,1,2,3,4,5,6,7,8,9,10 };
public static List<int> intList = new List<int>(); //The list also has same elements as the array.

注意:这同样适用于intList。

可以通过
.ToList()
方法将intArray解析为列表。列表有一个名为
GetRange(int index,int count)的方法

可以通过
.ToList()
方法将intArray解析为列表。列表有一个名为
GetRange(int-index,int-count)的方法

您可以使用LINQ以及和的组合:


它也适用于intList。

您可以使用LINQ以及和的组合:


它也适用于intList。

:OP澄清后,需要的是按索引列出的元素。以下解决方案按值范围提供元素。我选择把这个留给任何可能最终发现它有用的人

我认为下列方法会奏效:

var fromArray = intArray.SkipWhile(i => i < lower).TakeWhile(i => i <= upper).ToArray();
var fromList = intList.SkipWhile(i => i < lower).TakeWhile(i => i <= upper).ToList();
…通过使用
替换
,然后应用
where T:struct
约束,您可以使扩展方法适用于任何结构值

编辑 对我以前未测试的代码进行了以下更正:

class Program
{
    private static int[] intArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    static void Main(string[] args)
    {
        var rangeVals = intArray.GetValueRange(3, 8);

        Console.WriteLine(string.Join(",", rangeVals.Select(i => $"{i}")));
        Console.ReadKey();
    }
}

public static class Extensions
{
    public static IEnumerable<int> GetValueRange(this IEnumerable<int> source, int lower, int upper) {
        return source.SkipWhile(i => i < lower).TakeWhile(i => i <= upper);
    }

}
类程序
{
私有静态int[]intArray={0,1,2,3,4,5,6,7,8,9,10};
静态void Main(字符串[]参数)
{
var rangeVals=intArray.GetValueRange(3,8);
WriteLine(string.Join(“,”,rangeVals.Select(i=>$”{i}”);
Console.ReadKey();
}
}
公共静态类扩展
{
公共静态IEnumerable GetValueRange(此IEnumerable源,整数下限,整数上限){

return source.SkipWhile(i=>ii注意:在OP的澄清后,需要的是按索引的元素。以下解决方案按值范围提供元素。我选择将此留给可能最终发现它有用的任何人

我认为下列方法会奏效:

var fromArray = intArray.SkipWhile(i => i < lower).TakeWhile(i => i <= upper).ToArray();
var fromList = intList.SkipWhile(i => i < lower).TakeWhile(i => i <= upper).ToList();
…通过使用
替换
,然后应用
where T:struct
约束,您可以使扩展方法适用于任何结构值

编辑 对我以前未测试的代码进行了以下更正:

class Program
{
    private static int[] intArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    static void Main(string[] args)
    {
        var rangeVals = intArray.GetValueRange(3, 8);

        Console.WriteLine(string.Join(",", rangeVals.Select(i => $"{i}")));
        Console.ReadKey();
    }
}

public static class Extensions
{
    public static IEnumerable<int> GetValueRange(this IEnumerable<int> source, int lower, int upper) {
        return source.SkipWhile(i => i < lower).TakeWhile(i => i <= upper);
    }

}
类程序
{
私有静态int[]intArray={0,1,2,3,4,5,6,7,8,9,10};
静态void Main(字符串[]参数)
{
var rangeVals=intArray.GetValueRange(3,8);
WriteLine(string.Join(“,”,rangeVals.Select(i=>$”{i}”);
Console.ReadKey();
}
}
公共静态类扩展
{
公共静态IEnumerable GetValueRange(此IEnumerable源,整数下限,整数上限){

return source.SkipWhile(i=>ii非常感谢你们的建议和回答。我非常感谢你们的时间和分享的知识。在对我的程序进行了一些强调之后,我找到了一种实现所需功能的简单方法。下面是我的C代码:

class Program
{
   //Author         : Mudrak Patel
   //Github username: mudrakpatel

    static void Main(string[] args)
    {
        List<int> intList = new List<int>();
        List<double> doubleList = new List<double>();
        for (int index = 0; index < 12; index++)
        {
            intList.Add(index);
            doubleList.Add((double)index);
        }
        Console.WriteLine("\nAll elements of integer array\n--------------------------------------------------------");
        DisplayArray(intList.ToArray());
        Console.WriteLine("\nAll elements of double array\n--------------------------------------------------------");
        DisplayArray(doubleList.ToArray());
        Console.WriteLine("\nAll elements of integer array from 3rd place to 8th place\n--------------------------------------------------------");
        DisplayArray(intList.ToArray(), 3, 8);
        Console.WriteLine("\nAll elements of double array from 3rd place to 8th place\n--------------------------------------------------------");
        DisplayArray(doubleList.ToArray(), 3, 8);
    }

    //Overloaded DisplayArray method
   public static void DisplayArray<Type>(Type[] inputCollectionObject)
    {
        for (int index = 0; index < inputCollectionObject.Length; index++)
        {
            Console.WriteLine("Element: {0,2}", inputCollectionObject[index]);
        }
        Console.WriteLine("-----------------------------------------------");
    }

    public static void DisplayArray<Type>(Type[] inputCollectionObject, int lowIndex , int highIndex)
    {
        var selectedElements = new List<int>();
        for (int index = lowIndex; index < highIndex; index++)
        {
            selectedElements.Add(Convert.ToInt32(inputCollectionObject[index]));
        }

        Console.WriteLine("The elements within the range you specified:\n--------------------------------------------------");
        foreach (var element in selectedElements)
        {
            Console.WriteLine("Element: {0,2}", element);
        }
    }
}
类程序
{
//作者:Mudrak Patel
//Github用户名:mudrakpatel
静态void Main(字符串[]参数)
{
List intList=新列表();
List doubleList=新列表();
对于(int-index=0;index<12;index++)
{
intList.Add(索引);
doubleList.Add((双)索引);
}
Console.WriteLine(“\n整数数组的所有元素\n--------------------------------------------------------------------------------”);
DisplayArray(intList.ToArray());
Console.WriteLine(“\n双数组的所有元素\n------------------------------------------------------------------------------------------”);
DisplayArray(doubleList.ToArray());
Console.WriteLine(“\n整数数组中从第3位到第8位的所有元素\n---------------------------------------------------------------------”;
DisplayArray(intList.ToArray(),3,8);
Console.WriteLine(“\n从第3位到第8位的双数组的所有元素\n---------------------------------------------------------------------------”;
DisplayArray(doubleList.ToArray(),3,8);
}
//重载显示数组方法
公共静态void显示数组(类型[]inputCollectionObject)
{
for(int index=0;index
非常感谢你们的建议和回答。我非常感谢你们的时间和分享的知识。在对我的程序进行了一些强调之后,我找到了一种实现所需功能的简单方法。下面是我的C代码:

class Program
{
   //Author         : Mudrak Patel
   //Github username: mudrakpatel

    static void Main(string[] args)
    {
        List<int> intList = new List<int>();
        List<double> doubleList = new List<double>();
        for (int index = 0; index < 12; index++)
        {
            intList.Add(index);
            doubleList.Add((double)index);
        }
        Console.WriteLine("\nAll elements of integer array\n--------------------------------------------------------");
        DisplayArray(intList.ToArray());
        Console.WriteLine("\nAll elements of double array\n--------------------------------------------------------");
        DisplayArray(doubleList.ToArray());
        Console.WriteLine("\nAll elements of integer array from 3rd place to 8th place\n--------------------------------------------------------");
        DisplayArray(intList.ToArray(), 3, 8);
        Console.WriteLine("\nAll elements of double array from 3rd place to 8th place\n--------------------------------------------------------");
        DisplayArray(doubleList.ToArray(), 3, 8);
    }

    //Overloaded DisplayArray method
   public static void DisplayArray<Type>(Type[] inputCollectionObject)
    {
        for (int index = 0; index < inputCollectionObject.Length; index++)
        {
            Console.WriteLine("Element: {0,2}", inputCollectionObject[index]);
        }
        Console.WriteLine("-----------------------------------------------");
    }

    public static void DisplayArray<Type>(Type[] inputCollectionObject, int lowIndex , int highIndex)
    {
        var selectedElements = new List<int>();
        for (int index = lowIndex; index < highIndex; index++)
        {
            selectedElements.Add(Convert.ToInt32(inputCollectionObject[index]));
        }

        Console.WriteLine("The elements within the range you specified:\n--------------------------------------------------");
        foreach (var element in selectedElements)
        {
            Console.WriteLine("Element: {0,2}", element);
        }
    }
}
类程序
{
//作者:Mudrak Patel
//Github用户名:mudrakpatel
静态void Main(字符串[]参数)
{
List intList=新列表();
List doubleList=新列表();
对于(int-index=0;index<12;index++)
{
intList.Add(索引);
doubleList.Add((双)索引);
}
Console.WriteLine(“\n整数数组的所有元素\n--------------------------------------------------------------------------------”);
DisplayArray(intList.ToArray());
Console.WriteLine(“\n双数组的所有元素\n------------------------------------------------------------------------------------------”);
DisplayArray(doubleList.ToArray());
控制台,Wri