Mohanapriya R Mohanapriya R
Updated date Mar 15, 2024
In this article, we will learn how to convert arrays to IEnumerable in C#.

Converting Array to IEnumerable in C#

The differences between arrays and IEnumerable are

  • Arrays: Arrays in C# represent a fixed-size collection of elements of the same type. They offer quick access to elements via indexing but lack the flexibility of dynamically resizing.
  • IEnumerable: IEnumerable is an interface that defines a method for iterating over a collection of elements. It provides a generic way to traverse through various types of collections, enabling operations like filtering, mapping, and aggregation.

To convert an array to IEnumerable in C#, you can leverage the IEnumerable<T> interface, which is implemented by arrays implicitly. This allows you to treat arrays as IEnumerable collections.

using System;
using System.Collections.Generic;

class Program
    static void Main()
        // Define an array of integers
        int[] numbers = { 1, 2, 3, 4, 5 };

        // Convert array to IEnumerable
        IEnumerable<int> enumerableNumbers = numbers;

        // Iterate over the IEnumerable
        foreach (int num in enumerableNumbers)



Comments (0)

There are no comments. Be the first to comment!!!