✅ Help with Simplifying Method further
Anyway to make this code smaller so that i dont have to write "new List<Avalonia.Point>" and "new Avalonia.Point(x,y)" so many times?
Preferably wanting a function where i can parse as many points into the method as i need.
I have already added a method to create the line but it still contains too much rubbish repetitive code
5 Replies
1. use a collection initializer instead of
new List<T>
2. use target-typed new for the elements?
3. write a helper method that takes in a collection of tuples and returns a list of points?
3 like List<Avalonia.Point> FromTuples(params (int, int)[] tuples)
looking closely it doesn't look like the list is necessary at all, you're just using that as a way to get a collection of points into the other method
so you could do 3 by just putting that conversion logic in that method
public Polyline drawToPanel(params (int, int)[] points)
If you are using Visual Studio, it should already be giving you suggestions for simplifying the code that creates the list
I do get the “Can be simplified” stuff but this one didn’t have that. It just looks too ugly having new this new that all over the place
Instead of looping over points to add them to
newObject
's collection, use the initializer and LINQ
Assuming LINQ is even necessary hereI’m fairly new to Avalonia and even list creation in general so it was honestly just a working method at the time
I see what you mean. Putting a list into another list can be done inside the method rather than returning it.