1

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?

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Kaja
  • 2,962
  • 18
  • 63
  • 99

5 Answers5

2

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); 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • IListBlobItem is interface. `new IListBlobItem` is wrong syntax – Roma Ruzich Nov 05 '19 at 09:46
  • 1
    @Roma Ruzich: yes, `new IListBlobItem` is wrong syntax, but `new IListBlobItem[0]` is *not*: it's an *array* of `Length = 0` of `IListBlobItem` items – Dmitry Bychenko Nov 05 '19 at 09:48
  • Ops, sorry. But, is it better to declare the variable with a default value or not? – Roma Ruzich Nov 05 '19 at 09:56
  • @Roma Ruzich: `default(IEnumerable)` will return `null` which is *not a good practice* in case of collections (empty collection is a better choice) – Dmitry Bychenko Nov 05 '19 at 10:39
  • Explain, please, why empty collection is better then `null`. – Roma Ruzich Nov 05 '19 at 10:57
  • @Roma Ruzich: https://stackoverflow.com/questions/1969993/is-it-better-to-return-null-or-empty-collection – Dmitry Bychenko Nov 05 '19 at 11:06
  • thank you. I read this post. But there is no explanation why empty collection is better. I think that `null` is better because: 1) no place on heap 2) when you try to do some manipulation with `null`-collection you take exception – Roma Ruzich Nov 05 '19 at 11:16
  • 1
    @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 Nov 05 '19 at 11:26
  • 1
    @ Dmitry Bychenko, thanks thanks for the explanation – Roma Ruzich Nov 05 '19 at 11:35
2

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

Jakob Möllås
  • 4,239
  • 3
  • 33
  • 61
2

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.

1

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

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

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

Ray Krungkaew
  • 6,652
  • 1
  • 17
  • 28
1

Set IEnumerable blobs like property in this way:

IEnumerable<IListBlobItem> blobs{get;set;}
BASKA
  • 311
  • 4
  • 15