
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to create an array with non-default repeated values in C#?
We can create an array with non-default values using Enumerable.Repeat(). It repeated a collection with repeated elements in C#. Firstly, set which element you want to repeat and how many times.
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
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
- Related Articles
- How to create a vector with repeated values in R?
- Mapping an array to a new array with default values in JavaScript
- How to create a data frame with a column having repeated values in R?
- How to create an array with random values with the help of JavaScript?
- Convert array with duplicate values to object with number of repeated occurrences in JavaScript
- Default array values in Java
- JavaScript: How to filter out Non-Unique Values from an Array?
- How to initialize an array in Kotlin with values?
- How to create a repeated values column with each value in output selected randomly in R data frame?
- How to validate if an element in an array is repeated? - JavaScript
- How can we add columns with default values to an existing MySQL table?
- Create an array class with possibly masked values and fill in the masked values in Numpy
- A shorthand array notation in C for repeated values?
- How to set default values when destructuring an object in JavaScript?
- How to create an ordered array from values that have an order number in JavaScript?

Advertisements