通过使用不同类型的
问题描述:
重载函数我有这样的功能:通过使用不同类型的
public static Mesh MeshFromPolylines(List<Polyline> nurbsCurves, int type, bool weld)
{
..code..
}
,我已超载:
public static Mesh MeshFromPolylines(Polyline[] nurbsCurves, int type, bool weld)
{
..code..
}
有没有什么办法来写不复制第二个功能粘贴相同的代码? 这两个函数里面都有完全相同的代码。只是差异是输入List<Polyline>
和Polyline[]
。
答
一种单一的方法会工作,如果这将有签名:
public static Mesh MeshFromPolylines(IEnumerable<Polyline> nurbsCurves, int type, bool weld)
{
}
它将既接受数组和列表。或者至少你可以从你的两个方法中调用这个方法(如果你出于某种原因需要两个带有指定参数类型的方法)。
你可能得修改,虽然在方法体,例如获得通过索引的元素,你需要做的nurbsCurves.ElementAt(i)
代替nurbsCurves[i]
+0
感谢作为一种魅力,我不需要改变任何东西。 – Petras
答
如果你使用LINQ,你可以做这样的事情
public static Mesh MeshFromPolylines (List <Polyline> list, int type, bool weld)
{
MeshFromPolylines (list.ToArray(), type, weld);
}
你可以做相反的方式为好,请检查:List .ToArray(),Enumerable.ToList()
答
您可以将阵列Polyline[]
列出List<Polyline>
的d调用基本函数并通过列表。请检查下面的例子:
public static Mesh MeshFromPolylines(Polyline[] nurbsCurves, int type, bool weld)
{
MeshFromPolylines(nurbsCurves.ToList(), type, weld);
}
https://*.com/questions/4482557/what-interfaces-do-all-arrays-implement-in-c检查这个问题,字母“L “是固体原则(https://*.com/questions/13692126/cant-seem-to-understand-solid-principles-and-design-patterns) – demo