Petras
Petras

Reputation: 183

Overloading function by using different type

I have a function like this:

 public static Mesh MeshFromPolylines(List<Polyline> nurbsCurves, int type, bool weld) 
{ 
..code.. 
}

then I have overloading:

 public static Mesh MeshFromPolylines(Polyline[] nurbsCurves, int type, bool weld) 
{ 
..code.. 
}

Is there any way to write second function without copy paste the same code? Both function has totally the same code inside. Just the difference is input List<Polyline> and Polyline[].

Upvotes: 1

Views: 66

Answers (3)

Antoine Thiry
Antoine Thiry

Reputation: 2442

If you use LINQ, you can do something like this

public static Mesh MeshFromPolylines  (List <Polyline> list, int type, bool weld)
{
    MeshFromPolylines (list.ToArray(), type, weld);
}

You could do the opposite way as well, please check this : List .ToArray (), Enumerable.ToList ()

Upvotes: 0

evilkos
evilkos

Reputation: 597

One single method will work if it'll have the signature:

public static Mesh MeshFromPolylines(IEnumerable<Polyline> nurbsCurves, int type, bool weld) 
{ 
}

It will accept both the array and the list. Or at least you can call this one from both your methods (if you need two methods with your specified parameter types for some reason).

You'll probably have to modify the method body though, for example to get an element by index you'll need to do nurbsCurves.ElementAt(i) instead of nurbsCurves[i]

Upvotes: 5

csharpbd
csharpbd

Reputation: 4066

You can convert Array Polyline[] to List List<Polyline> and call the base function and pass the List. Please check below example:

public static Mesh MeshFromPolylines(Polyline[] nurbsCurves, int type, bool weld) 
{ 
    MeshFromPolylines(nurbsCurves.ToList(), type, weld);
}

Please check this for Array to List conversion.

Upvotes: 0

Related Questions