C# Single() 方法


使用 Single() 方法仅获取序列的一个元素。

假设我们有一个只有一个元素的字符串数组。

string[] str = { "one" };

现在,获取该元素。

str.AsQueryable().Single();

以下是我们的代码。

示例

 实时演示

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      string[] str = { "one" };
      string res = str.AsQueryable().Single();
      Console.WriteLine(res);
   }
}

输出

one

更新于: 2020 年 6 月 23 日

2K+ 次浏览

启动您的 职业生涯

通过完成此课程获得认证

开始
广告
© . All rights reserved.