Skip to content

Instantly share code, notes, and snippets.

@jagregory
Created February 14, 2012 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jagregory/1825592 to your computer and use it in GitHub Desktop.
Save jagregory/1825592 to your computer and use it in GitHub Desktop.
Ways of handling null collections and AddRange
// a)
if (items != null)
collection.AddRange(items)
// b)
collection.AddRange(items ?? new Foo[0])
@flq
Copy link

flq commented Feb 14, 2012

If no other option, I'd take a), as it seems awkward to add an empty array just for succintness of code. Optionally a "TryAdd" extension method on IList would be in order :)

@robashton
Copy link

if(!Object.ReferenceEquals(items, null)
  collection.AddRange(items);

:P

@gshutler
Copy link

I agree with @flq

@robashton
Copy link

Oh, and with the serious side, I'd be with @flq - I'd not bother with adding an empty array, and I disagree with Hadi - I make it a goal to avoid adding braces to my code. Forces the creation of methods a lot of the time and therefore increases readability.

@jagregory
Copy link
Author

@robashton I'm trying to make the code more succinct, not less.

@ashic
Copy link

ashic commented Feb 14, 2012

if(items == null)
items = new Foo[0];

collection.AddRange(items);

@ashic
Copy link

ashic commented Feb 14, 2012

Or even:
items = items ?? new Foo[0];
collection.AddRange(items);

@robashton
Copy link

@ashic still completely pointless

@ijrussell
Copy link

I agree with @flq.

@robashton
Copy link

I know, I know, I know!!

public static class IEnumerableExtensions {
    public static void AddTo<T>(this IEnumerable<T> self, ICollection<T> destination) {
        if(self != null) destination.AddRange(self);
    }
}

and

items.AddTo(collection);

What do I win?

@ashic
Copy link

ashic commented Feb 14, 2012

what...nobody for good old
if(null != items) collection.AddRange(items);
?

@jagregory
Copy link
Author

@ashic Same difference there really, except yours is more verbose than both :)

I personally prefer B, mainly because it's got a bit less indentation and the AddRange is a little more focal; that being said, having the new array is a bit of a shame. I could use Enumerable.Empty() which will only allocate a new array the first time around, but that just isn't as neat and tidy.

@jagregory
Copy link
Author

@robashton I think that should be collection.NullSafeAddRange(items)?

@ashic
Copy link

ashic commented Feb 14, 2012

Just use Rob's extension method...nice and clean...and what I use on "real" projects ;)

@ashic
Copy link

ashic commented Feb 14, 2012

@jagregory collection.CheckIfThisIsNullAndIfThisIsNotAddTheItemsInTheCollectionToMe(items); [Something Neil might write ;) ]

@robashton
Copy link

@jagregory I don't like it, not enough job security

@leniel
Copy link

leniel commented Mar 19, 2014

Small correction to @robashton's extension because ICollection doesn't have AddRange:

using System.Collections.Generic;

public static class IEnumerableExtensions
{
    public static void AddTo<T>(this IEnumerable<T> self, List<T> destination)
    {
        if(self != null) destination.AddRange(self);
    }
}

@RodionOrets
Copy link

@leniel How do you call this extension method on self if self is null? It seems like it would cause some exception))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment