使用 C# 获取数组中最后一个元素的程序
programmingserver side programmingcsharp更新于 2025/9/1 8:22:17
声明一个数组并添加元素。
int[] val = { 5, 8, 15, 25, 40, 55, 80, 100 };
现在,使用 Queryable Last() 方法获取最后一个元素。
val.AsQueryable().Last();
让我们看看完整的代码。
示例
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
int[] val = { 5, 8, 15, 25, 40, 55, 80, 100 };
int last_num = val.AsQueryable().Last();
Console.WriteLine("Last element: "+last_num);
}
}
输出
Last element: 100

