I'm new to C# and have been struggling to find an idiomatic way to initialize a list within a constructor.
Related questions that don't quite solve the issue:
This works, but with a flaw:
class Datapoint { public bool Debug { get; set; } public string Pattern { get; private set; } // I would prefer to initialize this list in the constructor public List<Dictionary<string, dynamic>> operations = new List<Dictionary<string, dynamic>>(); // constructor public Datapoint(bool debug = false, string pattern = "" // I would prefer operations to go here // but the following doesn't work: // List<Dictionary<string, dynamic>> operations = // new List<Dictionary<string, dynamic>>() ) { Debug = debug; Pattern = pattern; } } // Let's define some Datapoints class Definitions { public static Datapoint turtles = new Datapoint ( pattern: @"turtle pattern", // I would prefer operations to go here ) { operations = { new Dictionary<string, dynamic> { ["func"] = "stitch_lines" } } }; } The flaw is that I cannot set operations as private, otherwise I get an error when creating turtles.
Ideally I would like operations to be a parameter of the constructor, but I am missing something as every combination I try yields this error:
Default parameter value for operations must be a compile-time constant.
Thanks in advance for any insights.
List<Dictionary<string, dynamic>> operations = nulland then check fornullin the constructor.