Converting Array to IEnumerable<T> in C#

Converting Array to IEnumerable<T> in C#

To convert an array to an IEnumerable<T> in C#, you can simply use the IEnumerable<T> constructor, passing in the array as an argument. Here's an example:

int[] array = { 1, 2, 3, 4, 5 }; IEnumerable<int> enumerable = new List<int>(array); // Or, using the extension method AsEnumerable(): IEnumerable<int> enumerable2 = array.AsEnumerable(); 

In this example, we create an array of integers and then use the List<T> constructor to create a new list of integers from the array. Since List<T> implements IEnumerable<T>, we can assign the list to an IEnumerable<int> variable.

Alternatively, you can use the AsEnumerable() extension method, which is a shorthand way of creating an IEnumerable<T> from an array or other collection.

Examples

1. "C# convert array to IEnumerable"

Code:

using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { int[] intArray = { 1, 2, 3, 4, 5 }; IEnumerable<int> enumerable = intArray.AsEnumerable(); // Example usage foreach (var number in enumerable) { Console.WriteLine(number); } } } 

Description:

Demonstrates the use of AsEnumerable() to convert an array of integers to an IEnumerable<int>.

2. "Convert array to IEnumerable with LINQ"

Code:

using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { string[] stringArray = { "apple", "banana", "cherry" }; IEnumerable<string> enumerable = stringArray.AsEnumerable(); // Example usage foreach (var fruit in enumerable) { Console.WriteLine(fruit); } } } 

Description:

Illustrates how to use LINQ's AsEnumerable() to convert a string array to an IEnumerable<string>.

3. "C# array to IEnumerable conversion using ToList()"

Code:

using System; using System.Collections.Generic; class Program { static void Main() { char[] charArray = { 'a', 'b', 'c' }; IEnumerable<char> enumerable = charArray.ToList(); // Example usage foreach (var character in enumerable) { Console.WriteLine(character); } } } 

Description:

Shows how to convert an array of characters to IEnumerable<char> using the ToList() method.

4. "C# convert array to IEnumerable with Array.ForEach"

Code:

using System; class Program { static void Main() { double[] doubleArray = { 1.5, 2.5, 3.5 }; var list = new List<double>(); Array.ForEach(doubleArray, list.Add); // Example usage foreach (var number in list) { Console.WriteLine(number); } } } 

Description:

Demonstrates converting an array of doubles to IEnumerable<double> using Array.ForEach to add elements to a list.

5. "C# convert array to IEnumerable with yield return"

Code:

using System; using System.Collections.Generic; class Program { static void Main() { decimal[] decimalArray = { 1.2m, 2.3m, 3.4m }; IEnumerable<decimal> enumerable = GetEnumerable(decimalArray); // Example usage foreach (var number in enumerable) { Console.WriteLine(number); } } static IEnumerable<decimal> GetEnumerable(decimal[] array) { foreach (var item in array) { yield return item; } } } 

Description:

Illustrates using yield return to convert an array of decimals to IEnumerable<decimal>.

6. "C# convert array to IEnumerable with Enumerable.Range"

Code:

using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { int[] intArray = { 5, 10, 15 }; IEnumerable<int> enumerable = Enumerable.Range(0, intArray.Length).Select(i => intArray[i]); // Example usage foreach (var number in enumerable) { Console.WriteLine(number); } } } 

Description:

Shows how to use Enumerable.Range to convert an array of integers to IEnumerable<int>.

7. "C# array to IEnumerable using Select extension method"

Code:

using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { long[] longArray = { 100, 200, 300 }; IEnumerable<long> enumerable = longArray.Select(x => x); // Example usage foreach (var number in enumerable) { Console.WriteLine(number); } } } 

Description:

Illustrates how to convert an array of longs to IEnumerable<long> using the Select extension method.

8. "C# array to IEnumerable with custom conversion method"

Code:

using System; using System.Collections.Generic; class Program { static void Main() { string[] stringArray = { "one", "two", "three" }; IEnumerable<string> enumerable = ConvertArrayToEnumerable(stringArray); // Example usage foreach (var item in enumerable) { Console.WriteLine(item); } } static IEnumerable<T> ConvertArrayToEnumerable<T>(T[] array) { foreach (var item in array) { yield return item; } } } 

Description:

Demonstrates creating a custom method ConvertArrayToEnumerable to convert any array to IEnumerable<T>.

9. "C# convert array to IEnumerable with ToArray()"

Code:

using System; using System.Collections.Generic; class Program { static void Main() { DateTime[] dateArray = { DateTime.Now, DateTime.Now.AddDays(1), DateTime.Now.AddDays(2) }; IEnumerable<DateTime> enumerable = dateArray.ToArray(); // Example usage foreach (var date in enumerable) { Console.WriteLine(date); } } } 

Description:

Illustrates converting an array of DateTime values to IEnumerable<DateTime> using the ToArray() method.

10. "C# convert array to IEnumerable with Array.ConvertAll"

Code:

using System; using System.Collections.Generic; class Program { static void Main() { byte[] byteArray = { 10, 20, 30 }; IEnumerable<byte> enumerable = Array.ConvertAll(byteArray, b => b); // Example usage foreach (var value in enumerable) { Console.WriteLine(value); } } } 

Description:

Demonstrates using Array.ConvertAll to convert an array of bytes to IEnumerable<byte>.


More Tags

excel-2013 timeit wampserver qt5 entity-attribute-value status multi-tenant azure-devops-rest-api windows-10-desktop dead-code

More C# Questions

More Pregnancy Calculators

More Bio laboratory Calculators

More Organic chemistry Calculators

More Internet Calculators