C# 程式返回具有重複元素的集合


若要在 C# 中返回具有重複元素的集合,請使用 Enumerable.Repeat 方法。

它是 System.Linq 名稱空間的一部分。

假設你需要重複一個數字兩次,為此設定該數字和重複頻率。

Enumerable.Repeat(50, 2);

現在將其分配給一個變數並顯示它。

示例

 真實演示

using System;
using System.Linq;
class Demo {
   static void Main() {
      // repeating element 50, two times
      var num = Enumerable.Repeat(50, 2);
      // displayig repeating elements
      foreach (int ele in num) {
         Console.WriteLine(ele);
      }
   }
}

輸出

50
50

更新日期:2020-06-23

253 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.