Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set an empty iCollection c#

Tags:

c#

icollection

I have three collections:

private ICollection<FPTAssetClassAsset> wrassets;
private ICollection<FPTFundAsset> wrfunds;
private ICollection<FPTManagedStrategyAsset> wrstrats;

If a foreach loop returns 0 objects, the collections don't get set and are therefore null. When i add this icollection (Union) to another icollection it fails with: "Value cannot be null" because the icollection is null, rather than being Empty. How can i set this collection as empty instead?

Loop:

    public void GetWrapperAssets(FPT fpt)
    {
        foreach (var w in fpt.CouttsPositionSection.Wrappers
        .Union(fpt.StandAloneSection.Wrappers)
        .Union(fpt.BespokePropositionSection.Wrappers)
        .Union(fpt.NonCouttsPositionSection.Wrappers)
        )
        {
            foreach (var a in w.UnderlyingAssets.OfType<FPTManagedStrategyAsset>())
            {
                wrstrats.Add(a);
            }
            foreach (var a in w.UnderlyingAssets.OfType<FPTAssetClassAsset>())
            {
                wrassets.Add(a);
            }
            foreach (var a in w.UnderlyingAssets.OfType<FPTFundAsset>())
            {
                wrfunds.Add(a);
            }
        }
    }
like image 560
Marc Howard Avatar asked Jan 30 '14 16:01

Marc Howard


People also ask

How do you instantiate an ICollection?

ICollection is an interface, you can't instantiate it directly. You'll need to instantiate a class that implements ICollection ; for example, List<T> . Also, the ICollection interface doesn't have an Add method -- you'll need something that implements IList or IList<T> for that.

How do I return empty ICollection in C#?

To handle empty collections, use the DefaultIfEmpty() method in C#. If an array is empty, then using this method will show the default method instead of displaying an error.

How can you tell if an ICollection is empty?

Solution — To check if the collection is not empty, there are multiple ways. The most common way is to check if collection count > 0. Another way is to check if the collection has at least one element by checking Collection. Any() which returns true when it finds first element.

What is ICollection in C#?

The ICollection interface is the base interface for classes in the System. Collections namespace. Its generic equivalent is the System. Collections. Generic.


3 Answers

You can use Array.Empty<T> if you're just looking for something that won't crash upon a read until you can get around to replacing it with something you use in earnest:

private ICollection<FPTAssetClassAsset> wrassets      = Array.Empty<FPTAssetClassAsset>();
private ICollection<FPTFundAsset> wrfunds             = Array.Empty<FPTFundAsset>();
private ICollection<FPTManagedStrategyAsset> wrstrats = Array.Empty<FPTManagedStrategyAsset>();
like image 97
Caius Jard Avatar answered Sep 23 '22 09:09

Caius Jard


Well, you can always check for null before adding. Or you can turn it into a property:

private ICollection<FPTAssetClassAsset> wrassets
{ 
    get { return _wrassets == null ? new List<FPTAssetClassAsset>() : _wrassets; }
}
private ICollection<FPTAssetClassAsset> _wrassets;
like image 32
Theodoros Chatzigiannakis Avatar answered Sep 23 '22 09:09

Theodoros Chatzigiannakis


Initialise your collections before the foreach loop, this way they will always have a value:

private ICollection<FPTAssetClassAsset> wrassets = new Collection<FPTAssetClassAsset>();
private ICollection<FPTFundAsset> wrfunds = new Collection<FPTFundAsset>();
private ICollection<FPTManagedStrategyAsset> wrstrats = new Collection<FPTManagedStrategyAsset>();
like image 25
James Avatar answered Sep 23 '22 09:09

James