using an IEnumerable list outside of foreach loop

iterate through ienumerable linq
iterate through ienumerable c#
c# list foreach
how to add values to list in c# using foreach loop
foreach for list
cycle through ienumerable
c# for loop
c# array foreach

I am using IEnumerable in a for each loop as follows:

foreach (IListBlobItem blobItem in container.ListBlobs())
{        
    if (blobItem is CloudBlobDirectory)
    {
        CloudBlobDirectory directory = (CloudBlobDirectory)blobItem;
        IEnumerable<IListBlobItem> blobs = directory.ListBlobs(true);
    }                
}
await ProcessBlobs(blobs);

I would like to use blobsvariable outside of this foreach loop but I get this message: blobs doesnot exist in the current context

I decided to define blobs outside of the foreach loop:

IEnumerable<IListBlobItem> blobs = new IEnumerable<IListBlobItem>;

foreach (IListBlobItem blobItem in container.ListBlobs())
{           
    if (blobItem is CloudBlobDirectory)
    {
        //Console.WriteLine(blobItem.Uri);
        CloudBlobDirectory directory = (CloudBlobDirectory)blobItem;
        IEnumerable<IListBlobItem> blobs = directory.ListBlobs(true);                    
    }
}

but I get the error: can not create an instance of the abstract class or interface IEnumerable<IListBlobItem>

Do you have any idea how can I solve this problem?

You can declare blobs being an empty collection, say, array:

   // Empty
   IEnumerable<IListBlobItem> blobs = new IListBlobItem[0];

   foreach (IListBlobItem blobItem in container.ListBlobs())
   {
      if (blobItem is CloudBlobDirectory)
      {
          CloudBlobDirectory directory = (CloudBlobDirectory)blobItem;
          blobs = directory.ListBlobs(true);                    
      }            
   }

   // process either blobs from foreach or an empty collection
   await ProcessBlobs(blobs); 

Foreach On IEnumerable, I was working with my team on a regular working day. I was using foreach loop and one of my co-developers asked me if I used the IEnumerable collection� Accessing IEnumerable Model values outside the foreach at Razor view [Answered] RSS 10 replies Last post Jul 22, 2011 03:16 AM by amitpatel.it

Try using Enumerable.Empty<TResult>, like so:

IEnumerable<IListBlobItem> blobs = Enumerable.Empty<IListBlobItem>();

This will return an empty, non-null enumerable.

See .NET API Documentation

C# foreach statement, In most uses, foreach iterates an IEnumerable<T> expression where each the foreach statement block, you can break out of the loop by using the new List< int> { 0, 1, 1, 2, 3, 5, 8, 13 }; int count = 0; foreach (int element in� The foreach keyword basically just "rewrites" your code so it becomes a while loop calling MoveNext() and looking at Current, but the interface IEnumerable isn't needed. This is backed up by the C# Language Specification under section 15.8.4 The foreach statement

Set IEnumerable blobs like property in this way:

IEnumerable<IListBlobItem> blobs{get;set;}

Iterate through collections in C#, An iterator can be used to step through collections such as lists and arrays. In the following example, the first iteration of the foreach loop causes execution IEnumerable SomeNumbers() { yield return 3; yield return 5; yield return 8; } In C#, an iterator method cannot have any in , ref , or out parameters. ienumerable update in foreach loop. Ask Question Asked 7 years, 8 months ago. Active 7 years, 8 months ago. Viewed 1k times -5. 0. I have a code like this.

Use default to get default value. This will return null for reference type

IEnumerable<IListBlobItem> blobs = default(IEnumerable<IListBlobItem>);

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/default

Safe foreach loops with C#, public interface IEnumerable<out T> : IEnumerable We can use foreach on everythings that implements IEnumerable interface. Notice that ArrayList, as we remember, was array of objects of any type represented as list. using variable of the foreach loop outside foreach loop (4 answers) declare variable outside of the foreach loop (3 answers) Closed 2 years ago .

You are trying to create an object of an interface which is impossible. Instead, declare blobs as object an then convert it to IEnumerable<IListBlobItem>.

object blobs = null;

foreach (IListBlobItem blobItem in container.ListBlobs())
{           
    if (blobItem is CloudBlobDirectory)
    {
        //Console.WriteLine(blobItem.Uri);
        CloudBlobDirectory directory = (CloudBlobDirectory)blobItem;
        blobs = directory.ListBlobs(true);                    
    }
}
///usage:
///(IEnumerable<IListBlobItem>)blobs

Also, you can declare blobs as IEnumerable<IListBlobItem> which other answers cover.

C# IEnumerable Examples: LINQ, Lists and Arrays, Foreach: We use IEnumerable and the foreach-loop to access, in sequence, all items in a 2D array. We can abstract the loop itself out. Here: This example shows � The problem is that when you declare a variable inside a loop (or other closed statement) in c#, it only exists within that context. You need to declare recprec outside of your loop or put your if statement in the foreach loop depending on what you want to accomplish here.

forEach and Enumerables in C#, forEach and Enumerables in C# these ideas were incorporated into C# and check out how we can utilize them for 1 IEnumerable<string> iEnumerableOftoBeIterated Using a foreach loop will allow us to traverse the list. Note: Both of these approaches have the same goal: to allow the programmer to traverse a container, in our case a list. However, when we use the IEnumerator approach, it only works if the MoveNext() function is called. The main difference between IEnumerable and IEnumerator is that the latter retains the cursor's current state.

How to use an index with C#'s <code>foreach</code> loop? � Kodify, But that also means there's no index variable. How do we get one with the foreach loop? Let's find out. IN THIS ARTICLE: Use an index counter� The enumerator is like a “cursor” or a “bookmark” in the sequence. You can have multiple bookmarks, and moving any one of them enumerates over the collection independently of the others. Using this pattern, the C# equivalent of a foreach loop will look like the code shown in Figure 2.

C# foreach Loop Examples, Add(3); // Loop over list elements using foreach-loop. foreach (int element in list) receives an IEnumerable collection, which includes arrays (string[]) and Lists. First, using a while loop accessing the IEnumerator based object directly, and second, using a foreach loop accessing the enumerator through the IEnumerable interface. This first enumerator class, suitsEnumerator, will return each of the suits in a deck of cards. First notice that the class uses the IEnumerator interface. This is the base

Comments
  • Create a List<IListBlobItem>... Also you cannot have declaration twice...
  • Try: IEnumerable<IListBlobItem> blobs = null; ...
  • IEnumerable is an interface and you can't instantiate an interface. Instead, you can find a class that implements IEnumerable and use that. Maybe use Listand do IEnumerable<ILIstBlobItem> blobs = new List<IListBlobItem>();
  • IListBlobItem is interface. new IListBlobItem is wrong syntax
  • @Roma Ruzich: yes, new IListBlobItem is wrong syntax, but new IListBlobItem[0] is not: it's an array of Length = 0 of IListBlobItem items
  • Ops, sorry. But, is it better to declare the variable with a default value or not?
  • @Roma Ruzich: 1. when working with collection (often a lot of items), 4 or 8 bytes on a heap is not worth be optimized 2. usually, we don't want any NullReferenceException to be thrown, but just return - if we don't have any blob to process then await ProcessBlobs(blobs); shouldn't throw any exception but do nothing. ProcessBlobs can well be something like foreach (var blob in blobs) {...}, if blobs is null this (quite natural) implementation crashes but does as expected in case blobs is an empty collection
  • @ Dmitry Bychenko, thanks thanks for the explanation
  • default will return null, which is often not a good practice for collections (an empty collection usually is a better choice)
  • Agreed. Thanks for the comment.