Because
Add
is a void method, you can't use it directly in an initializer.
Change it to return the
MyDataPlot
instance, and it works:
public class MyDataPoint
{
public MyDataPoint(int x, int y) { X = x; Y = y; }
public int X { get; private set; } = 0;
public int Y { get; private set; } = 0;
}
public class MyDataPlot : IReadOnlyCollection<MyDataPoint>
{
public MyDataPoint this[int index] => _myDataPlot[index];
public MyDataPlot Add(int x, int y)
{
_myDataPlot.Add(new MyDataPoint(x, y));
return this;
}
public int Count => _myDataPlot.Count;
public IEnumerator<MyDataPoint> GetEnumerator() => _myDataPlot.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _myDataPlot.GetEnumerator();
private List<MyDataPoint> _myDataPlot = new List<MyDataPoint>();
}
public static class WorkWithData
{
public static void DoSomethingWithDataStructure() { }
private static Dictionary<string, MyDataPlot> _dataStructure = new Dictionary<string, MyDataPlot>()
{
{"One", new MyDataPlot().Add(1, 1) }
};
}