Reputation: 443
What is wrong with this code? I keep getting a StackOverlflowException
..
public class Places
{
public string Title { get; set; }
public string Content { get; set; }
public double Latitude { get; set; }
public double Longtitude { get; set; }
public List<Places> allPlaces = new List<Places>
{
new Places { Title = "test", Content = "test\ntest", Latitude = 52.23057, Longtitude = 5.84582 },
new Places { Title = "testt", Content = "dfsdf", Longtitude = 52.35589, Latitude = 4.92119 }
};
}
Upvotes: 1
Views: 1552
Reputation: 16121
Since allPlaces
is an instance field, it is initialized during construction of a Places
object. So you create a Places
object, which creates a List<Places>
, which creates another Places
object in its collection initializer, which creates another List<Places>
of its own... a never ending recursion.
You probably wanted to create a static allPlaces
field, which would create only one list. Add the static
keyword to the field as follows:
public static List<Places> allPlaces = ...
Upvotes: 5