Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging params and IList<T> constructors

Is there any way to merge these two constructors into one? Basically they accept the same array of Point3D type.

public Curve(int degree, params Point3D[] points) {} 

public Curve(int degree, IList<Point3D> points) {}

Thanks.

like image 452
abenci Avatar asked Aug 29 '16 12:08

abenci


1 Answers

If you want to have 2 different constructors you can:

public Curve(int degree, params Point3D[] points) : this(degree, (IList<Point3D>)points) { }
public Curve(int degree, IList<Point3D> points) { }

Or if you want only one constructor lets say the first then you can initialize like this:

new Curve(0,new List<Point3D>().ToArray());

By having one constructor calling the other you don't need to duplicate all your logic and you still enable both formats of initialization.


Though Array implements IList<T> one cannot remove the (IList<Point3D) due to compile error of: compiler ...... cannot call itself

enter image description here

like image 55
Gilad Green Avatar answered Oct 17 '22 03:10

Gilad Green