-1

I know I could wrap the entire foreach loop in an if statement, but I'm wondering if there's some object literal value replacement for new List<string>() so that the foreach skips executing when myList is null? In Javascript I could simply put [] after a coalesce to refer to an empty set that would stop the foreach loop.

List<string> myList = null;

foreach (var i in myList ?? new List<string>() )
    i.Dump();

Unnecessary background information that does not change the answer: List<Entity>() is my actual list that comes from ASP.NET MVC data binding, so I don't control creating it. I used the Entity Framework database models (POCOs) as an input to the Controller method like you would use a control function, setting parameters of the function to flow data to a table. The POCO comes from the database, so I don't control that either.

Just looking for some sort of empty object literal I could use to avoid running the loop without creating a new object.

I really think it's a C# flaw to throw an exception when myList is null. If it's null, there's nothing to do, so the loop should skip over.

Zachary Scott
  • 20,968
  • 35
  • 123
  • 205
  • 3
    Use [`Enumerable.Empty()`](https://msdn.microsoft.com/en-us/library/bb341042(v=vs.110).aspx). – dbc Dec 15 '17 at 19:18
  • @dbc That worked. post the answer and I'll mark it. Thanks – Zachary Scott Dec 15 '17 at 19:20
  • 1
    Or see [C# EmptyIfNull extension for any IEnumerable to return empty derived type](https://stackoverflow.com/q/34645963/3744182) or [Linq method to transform nulls into empty IEnumerable?](https://stackoverflow.com/q/6115524) if you need to make this check often. – dbc Dec 15 '17 at 19:20
  • @dbc you have a lot of hats – M Y Dec 15 '17 at 19:23
  • I just initialize my lists to new List(); in my model's constructors. It assures me that I don't have a null value. – Gilles Dec 15 '17 at 19:29
  • 1
    *I really think it's a C# flaw to throw an exception when myList is null.* - I tend to agree. I once had to fix a performance issue with dynamically updating a complex CAD model (200k+ geometric objects) where a measurable amount of time was spent counting many, many empty collections. By "measurable" I mean 1/40th of a second -- but the update had to be completed in 1/4 of a second so the time spent counting the collections, though a small part of the problem, actually mattered. But my experience is sort of unusual. – dbc Dec 15 '17 at 19:34
  • @Gilles unfortunately ASP.NET's data binding feeds the data to me. I don't think it executes constructor methods at least in this sense to set them to empty lists. – Zachary Scott Dec 15 '17 at 19:36
  • I really wish negative feedback was accompanied with an explanation from a named author. This answer actually solved my problem, was valuable to me, and probably valuable to others. – Zachary Scott Feb 18 '20 at 20:18

1 Answers1

3

Use Enumerable.Empty<string>().

See C# EmptyIfNull extension for any IEnumerable to return empty derived type or Linq method to transform nulls into empty IEnumerable<T>? for extension methods you could use if you need to make this check often.

dbc
  • 104,963
  • 20
  • 228
  • 340