Hi I have a class named Activity,
On a form I create its object array as,
Activity[] _actList;
And then do this,
List<Activity> termsList = _actList.ToList<Activity>();
since _actiList
is null its throwing the ArgumentNullException
.
So my question is,
How can I handle this exception?
Or is there a work around to get the same functionality ?
Please Help!
You need to check for null before calling ToList
var termsList = _actList == null ? null : _actList.ToList();
If you are using the C# 6.0 or later you can write the same in a shorter way:
var termsList = _actList?.ToList();
You could also catch the exception, but I don't recommend that in this case especially if it is a common scenario for the array to be null
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