当前位置: 首页 > 工具软件 > Go-LINQ > 使用案例 >

LINQ之Reverse

薛阳荣
2023-12-01

返回LINQ大全首页

Reverse()

反转序列中元素的顺序。
MSDN

using System.Linq;
using System.Collections;
using System.Collections.Generic;

public static class Program
{

    static void Main( string[] args )
    {
        int[]       dataA   = new int[]         { 0, 1, 2, 3, 4 };
        List<float> dataB   = new List<float>() { 1.5f, 1.3f, 3.2f };
        string[]    dataC   = new string[]      { "正一郎", "清次郎", "誠三郎", "征史郎" };

        IEnumerable<int>    dataA_R = dataA.Reverse();
        IEnumerable<float>  dataB_R = dataB.AsEnumerable().Reverse();
        IEnumerable<string> dataC_R = dataC.Reverse();

        System.Console.WriteLine( "dataA        :{0}", dataA.Text() );
        System.Console.WriteLine( "dataA Reverse:{0}", dataA_R.Text() );
        System.Console.WriteLine( "dataB        :{0}", dataB.Text() );
        System.Console.WriteLine( "dataB Reverse:{0}", dataB_R.Text() );
        System.Console.WriteLine( "dataC        :{0}", dataC.Text() );
        System.Console.WriteLine( "dataC Reverse:{0}", dataC_R.Text() );

        System.Console.ReadKey();
    }

    public static string Text( this IEnumerable i_source )
    {
        string text = string.Empty;
        foreach( var value in i_source )
        {
            text += string.Format( "[{0}], ", value );
        }
        return text;
    }
}

dataA :[0], [1], [2], [3], [4],
dataA Reverse:[4], [3], [2], [1], [0],
dataB :[1.5], [1.3], [3.2],
dataB Reverse:[3.2], [1.3], [1.5],
dataC :[正一郎], [清次郎], [誠三郎], [征史郎],
dataC Reverse:[征史郎], [誠三郎], [清次郎], [正一郎],
 类似资料: