In VB.Net, I use a variation of this, so when SearchModel is used, there is an empty list of tags ready to go.
Public Class SearchModel Public Property Tags As New List(Of TagDetails) End Class
A simple conversion leads to this, but the Tags are null:
public class SearchModel { public List<TagDetails> Tags { get; set; } }
Is this an acceptable way to create a Tags property while creating a new empty list?
public class SearchModel { public List<TagDetails> Tags = new List<TagDetails>(); }
Or should I go through this whole ceremony?
public class SearchModel { private List<TagDetails> _TagDetails; public List<TagDetails> Tags { get { return _TagDetails ?? (_TagDetails = new List<TagDetails>()); } set { _TagDetails = value; } } }
source share