Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code for adding to IEnumerable

Tags:

I have an enumerator like this

IEnumerable<System.Windows.Documents.FixedPage> page; 

How can I add a page (eg: D:\newfile.txt) to it? I have tried Add, Append, Concat etc But nothing worked for me.

like image 791
Sudha Avatar asked Apr 02 '13 09:04

Sudha


People also ask

How do I add items to IEnumerable?

What you can do is use the Add extension method to create a new IEnumerable<T> with the added value. var items = new string[]{"foo"}; var temp = items; items = items. Add("bar");

What is IEnumerable <>?

IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated. This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.

Is it possible to use Add or AddRange methods on IEnumerable?

Unfortunately, List<T>. AddRange isn't defined in any interface.

Can you use Linq on IEnumerable?

All LINQ methods are extension methods to the IEnumerable<T> interface. That means that you can call any LINQ method on any object that implements IEnumerable<T> . You can even create your own classes that implement IEnumerable<T> , and those classes will instantly "inherit" all LINQ functionality!


1 Answers

Yes, it is possible

It is possible to concatenate sequences (IEnumerables) together and assign the concatenated result to a new sequence. (You cannot change the original sequence.)

The built-in Enumerable.Concat() will only concatenate another sequence; however, it is easy to write an extension method that will let you concatenate a scalar to a sequence.

The following code demonstrates:

using System; using System.Collections.Generic; using System.Linq;  namespace Demo {     public class Program     {         [STAThread]         private static void Main()         {             var stringList = new List<string> {"One", "Two", "Three"};              IEnumerable<string> originalSequence = stringList;              var newSequence = originalSequence.Concat("Four");              foreach (var text in newSequence)             {                 Console.WriteLine(text); // Prints "One" "Two" "Three" "Four".             }         }     }      public static class EnumerableExt     {         /// <summary>Concatenates a scalar to a sequence.</summary>         /// <typeparam name="T">The type of elements in the sequence.</typeparam>         /// <param name="sequence">a sequence.</param>         /// <param name="item">The scalar item to concatenate to the sequence.</param>         /// <returns>A sequence which has the specified item appended to it.</returns>         /// <remarks>         /// The standard .Net IEnumerable extensions includes a Concat() operator which concatenates a sequence to another sequence.         /// However, it does not allow you to concat a scalar to a sequence. This operator provides that ability.         /// </remarks>          public static IEnumerable<T> Concat<T>(this IEnumerable<T> sequence, T item)         {             return sequence.Concat(new[] { item });         }     } } 
like image 81
Matthew Watson Avatar answered Sep 29 '22 22:09

Matthew Watson