C
C#14mo ago
mejobloggs

❔ Is it a problem to be creating lots of empty lists etc just to avoid null checks?

Over thousands of items, is this expensive or a problem? What is a better way to avoid null checks?
public class MyLists
{
public List<ThingOne> ListOne { get; set; } = new List<ThingOne>();

public List<ThingTwo> ListTwo { get; set; } = new List<ThingTwo>();

public List<ThingThree> ListThree { get; set; } = new List<ThingThree>();

public List<ThingFour> ListFour { get; set; } = new List<ThingFour>();

public List<ThingFive> ListFive { get; set; } = new List<ThingFive>();
}

for(int i = 0; i < 1000000; i++){

MyLists lists = LoadListData(i) ?? new MyLists();

//If no listdata, make a new MyLists so don't have to null check all the time
//MyLists also defaults each list to a new List to avoid null checks

//Is it a problem to be creating thousands of empty lists just to avoid null checks? Is there a better way?


//do some linq on lists
lists.ListOne.Where(...)
lists.ListTwo.FirstOrDefault(...)
lists.ListThree.Select(...)
//etc etc
}
public class MyLists
{
public List<ThingOne> ListOne { get; set; } = new List<ThingOne>();

public List<ThingTwo> ListTwo { get; set; } = new List<ThingTwo>();

public List<ThingThree> ListThree { get; set; } = new List<ThingThree>();

public List<ThingFour> ListFour { get; set; } = new List<ThingFour>();

public List<ThingFive> ListFive { get; set; } = new List<ThingFive>();
}

for(int i = 0; i < 1000000; i++){

MyLists lists = LoadListData(i) ?? new MyLists();

//If no listdata, make a new MyLists so don't have to null check all the time
//MyLists also defaults each list to a new List to avoid null checks

//Is it a problem to be creating thousands of empty lists just to avoid null checks? Is there a better way?


//do some linq on lists
lists.ListOne.Where(...)
lists.ListTwo.FirstOrDefault(...)
lists.ListThree.Select(...)
//etc etc
}
7 Replies
Thinker
Thinker14mo ago
thousands
I don't think this is inherently a problem, but if you're creating thousands of empty lists then... maybe? Really it depends on your application. If performance is absolutely critical them you definitely shouldn't be doing this. I think you should consider if these needs to be specifically lists, because these properties have setters which implies that they're intended to be set. Why can't they just be readonly and always have the initial list as their value?
mejobloggs
mejobloggs14mo ago
just checked whats happening in the actual program yes it could be readonly, but the data comes from database and sometimes data may not be available for each list etc so they legitimately could be null performance isnt particularly critical i just felt like i was doing something wrong generating thousands of empty lists just to save me from null checking and wondered if there was a better way
Mayor McCheese
Mayor McCheese14mo ago
Are you creating empty lists just to discard them later?
mejobloggs
mejobloggs14mo ago
if there was no data available yes just creating empty lists. i guess garbage collector discards them later
mejobloggs
mejobloggs14mo ago
It sounds like Enumerable.Empty<T>() or Array.Empty<T>() might be a better way, reading through here it sounds a more efficient equivilent https://stackoverflow.com/questions/8555865/is-there-an-empty-list-singleton-in-c
Stack Overflow
Is there an "Empty List" singleton in C#?
In C# I use LINQ and IEnumerable a good bit. And all is well-and-good (or at least mostly so). However, in many cases I find myself that I need an empty IEnumerable<X> as a default. That is, I
Mayor McCheese
Mayor McCheese14mo ago
It smells a bit tbh, at least in my book; you create a list on object creation just to reassign it with another list. This sounds like the object you're creating is overloaded with responsibility tbh.
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.