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);
}
}
}
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.
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.
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.
The ICollection interface is the base interface for classes in the System. Collections namespace. Its generic equivalent is the System. Collections. Generic.
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>();
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;
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>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With