How to fill an array with default values in C# ?

Published on July 5, 2015 by abundantcode

Filed under C#

Last modified July 5, 2015

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 3948 times

To fill an array with the default values in C# , you can use the Enumerable.Repeat method to do it.

How to fill an array with default values in C# ?

Below is a sample code illustrating the usage of the Enumerable.Repeat method to fill an array with default values.

using System;
using System.Collections.Generic;
using System.Linq;

namespace AbundantCodeConsoleApp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            string[] inputStr = Enumerable.Repeat("Abundantcode.com's MCA Lab programs", 10).ToArray();

            foreach (var item in inputStr)
                Console.WriteLine(item);
            Console.ReadLine();
        }
    }
}

image

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


x

You might be Interested in these Posts

C++ Program to display "Hello, World!"

Problem Write a program in C++ to display "Hello, World!" on the screen. C++ Program to...

C# and Lambda - Finding all the elements of an integer array less than 35

Here's a sample code snippet demonstrating how to find all the elements of an integer array that is ...

C# and LINQ - Finding all the elements of an integer array less than 35

Here's a sample code snippet demonstrating how to find all the elements of an integer array that is ...