I have a list of points which I am using to add points.
I have two approaches but I just want to know which one is better.
Following is the code for first approach:
List<Point> borderPoints = new List<Point>();
Point p1 = new Point(0, 0);
Point p2 = new Point(Width, 0);
borderPoints.AddRange(new[] { p1, p2 });
in the second approach I add the points like below:
borderPoints.Add(p1);
borderPoints.Add(p2);
I have to add the points four times, so which approach I should follow and why, or it doesn’t matter, it’s just the case of style ?
You are right.
In your situation it is more a case of your programming style.
Add()is simple and convenient in cycles whileAddRange()looks more elegant when used at once.Thinking about performance:
AddRange()adds an array of previously created tree nodes to the collection whileAdd()adds a new tree node to the collection (MSDN documentation).So if you are only adding just 4 of nodes or adding nodes infrequently, use the
Add().