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

王林
Release: 2023-09-23 22:45:06
forward
1157 people have browsed it

如何在 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();
   }
}
Copy after login

Output

10
10
10
10
10
Copy after login
Copy after login

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();
   }
}
Copy after login

Output

10
10
10
10
10
Copy after login
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template