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.