Is there an advantage in one of these approaches over the other as far as performance?
public class Person
{
public List<string> Favorites {get; set;} = new List<string>();
}
or
public class Person
{
public List<string> Favorites {get; set;} = Array.Empty<string>().ToList();
}
I understand that there are advantages of using Enumerable.Empty<> and Array.Empty<> when working with these types. But I’m wondering specifically about when the property is of type List<>. My instinct says that both approaches ultimately allocate a List object, so they are essentially equivalent. Thanks!
>Solution :
Array.Empty<>() is a very useful way of making empty arrays due to empty array optimization, but you can’t do better than new List() for an empty list. .ToList() eventually needs to invoke a list constructor anyway; and the cheapeast list constructor is the default constructor.
So, no point in Array.Empty<>().ToList().