Deborah's Developer MindScape

         Tips and Techniques for Web and .NET developers.

September 4, 2009


Filed under: C#,Lambda Expressions,LINQ,VB.NET @ 3:57 pm

The Enumerable class is new in .NET 3.5 and is part of the System.Linq namespace. It provides a set of static methods that allow you to query any object that implements IEnumerable, basically meaning any object that supports a for/each.

This post focuses on the Repeat method of the Enumerable class and some of the helpful things that this class can do for you.

NOTE: Be sure to set a reference to System.Core.

First, at the top of your code file, add this code.

In C#:

using System.Linq;

In VB:

Imports System.Linq

(Or for VB you can import a namespace using the project properties.)

You can then use the Enumerable.Repeat as shown in the following examples.

In C#:

// Initialize an array to -1 for each of 10 elements
int[] all1 = Enumerable.Repeat(-1, 10).ToArray();

// Initialize an array to "A" for each of 10 elements
string[] allA = Enumerable.Repeat("A", 10).ToArray();

// Convert a single value to an array
int value = 42;
int[] valueArray = Enumerable.Repeat(value, 1).ToArray();

// Generate 10 random numbers
Random rand = new Random();
int[] randomArray = Enumerable.Repeat(0, 10).Select(
                    i => rand.Next(0,10)).ToArray();

// Initialize a list with 5 Customer objects
List<Customer> custList = Enumerable.Repeat(
                    new Customer(), 10).ToList();

In VB:

‘ Initialize an array to -1 for each of 10 elements
Dim all1() As Integer = Enumerable.Repeat(-1, 10).ToArray()

‘ Initialize an array to "A" for each of 10 elements
Dim allA() As String = Enumerable.Repeat("A", 10).ToArray()

‘ Convert a single value to an array
Dim value As Integer = 42
Dim valueArray() As Integer = Enumerable.Repeat(value, 1).ToArray()

‘ Generate 10 random numbers
Dim rand As Random = New Random()
Dim randomArray() As Integer = Enumerable.Repeat(0, 10).Select( _
                    Function(i) rand.Next(0, 10)).ToArray()

‘ Initialize a list with 5 Customer objects
Dim custList As List(Of Customer) = Enumerable.Repeat( _
                    New Customer(), 10).ToList()

The first example initializes an array of 10 integers to –1.

The second example initializes an array of 10 strings to "A".

The third example takes a single value and creates an array from it. This is often necessary when a method requires an array instead of a single element.

The random number example provides another way to generate a random numbers. In this case, it generates 10 random numbers between 0 and 9. Note that this does not ensure uniqueness, so the number 2 for example could occur multiple times in the list.

The last example initializes a list of business objects to an empty set of objects. You could use object initializer syntax in this example to initialize all of the objects to some set of values, such as setting the State = "CA". This could be useful in setting up data for testing or prototyping.

Use the Repeat method any time you want to repeat values in an array or list.



  1.   Waleed El-Badry — September 4, 2009 @ 9:24 pm    Reply

    This is a very important feature to perform dummy repetition of the same data over and over and a strong replacement of the traditional loop since it is an inline function.

    Thanks Deborah. This post is beyond amazing

  2.   Mikael Svenson — June 12, 2010 @ 5:01 am    Reply

    List custList = Enumerable.Repeat(
    new Customer(), 10).ToList();

    This will put the same object reference into all 10 list items. If you change one object, then the other 9 will change as well, as they are the same object.

    Enumerable.Range(0, 10)
    .Select(x => new Customer())

    will work.

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2022 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs