Home  >  Article  >  Backend Development  >  How to create an array with non-default repeating values ​​in C#?

How to create an array with non-default repeating values ​​in C#?

王林
王林forward
2023-09-23 22:45:061153browse

如何在 C# 中创建具有非默认重复值的数组?

We can use Enumerable.Repeat() to create an array with non-default values. it Repeat a collection containing repeated elements in C#. First, set which element you Repeat as many times as you want.

Example 1

class Program{
   static void Main(string[] args){
      var values = Enumerable.Repeat(10, 5);
      foreach (var item in values){
         System.Console.WriteLine(item);
      }
      Console.ReadLine();
   }
}

Output

10
10
10
10
10

Example 2

is translated as:

Example 2

class Program{
   static void Main(string[] args){
      int[] values = Enumerable.Repeat(10, 5).ToArray();
      foreach (var item in values){
         System.Console.WriteLine(item);
      }
      Console.ReadLine();
   }
}

Output

10
10
10
10
10

The above is the detailed content of How to create an array with non-default repeating values ​​in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete