C# 具有不同返回类型的重载函数

C# 具有不同返回类型的重载函数,c#,overloading,C#,Overloading,我有两个同名函数,主要区别在于返回类型不同。我如何重载函数以使用相同的名称,因为有时我需要point3d或point3f数组,而以下函数名称因相同而产生错误: public static Point3d[] GetNGonCenters(this Mesh mesh) { Point3d[] centers = new Point3d[mesh.Ngons.Count]; for (int i = 0; i < mesh.Ngons.Count; i++)

我有两个同名函数,主要区别在于返回类型不同。我如何重载函数以使用相同的名称,因为有时我需要point3d或point3f数组,而以下函数名称因相同而产生错误:

public static Point3d[] GetNGonCenters(this Mesh mesh)
{
    Point3d[] centers = new Point3d[mesh.Ngons.Count];

    for (int i = 0; i < mesh.Ngons.Count; i++)
        centers[i] = mesh.Ngons.GetNgonCenter(i);

    return centers;
}

public static Point3f[] GetNGonCenters(this Mesh mesh)
{
    Point3f[] centers = new Point3f[mesh.Ngons.Count];

    for (int i = 0; i < mesh.Ngons.Count; i++)
        centers[i] = (Point3f)mesh.Ngons.GetNgonCenter(i);

    return centers;
}
publicstaticpoint3d[]GetNGonCenters(此网格)
{
Point3d[]centers=新的Point3d[mesh.Ngons.Count];
对于(int i=0;i
编译器无法知道您在调用什么。我建议让名字更具描述性,如:

public static Point3d[] GetNGonCenters3D(this Mesh mesh)



重载在这里不起作用,因为它们都使用相同的参数,编译器无法猜测您想要的返回类型。

如果您想要重载函数,您必须拥有一个名称相同但参数不同的函数。您没有重载函数,因为它们具有相同的名称和参数。您必须重命名函数或向其中一个函数添加新参数

您可以使用泛型:

public static T[] GetNGonCenters<T>(this Mesh mesh) where T : Point3d
{
    T[] centers = new T[mesh.Ngons.Count];

    for (int i = 0; i < mesh.Ngons.Count; i++)
        centers[i] = (T)mesh.Ngons.GetNgonCenter(i);

    return centers;
}
publicstatict[]GetNGonCenters(此网格),其中T:Point3d
{
T[]中心=新的T[mesh.Ngons.Count];
对于(int i=0;i

希望这有帮助。

您不能重载两个函数,唯一的区别是返回类型。C#使用参数列表作为上下文

两个想法:

  • 您可以让函数返回一个对象,并在调用中对其进行类型转换
  • 您可以在标头中包含一个伪变量来区分方法上下文
  • 这里有一个很好的链接,可以链接到一篇关于C#重载的论文。

    不能仅通过具有不同的返回类型来重载方法。当您按名称调用方法并为其提供参数时,就会找到该方法,而不是按您希望返回的对象

    考虑在以下示例中会发生什么(请注意,它不会编译):


    只能重载方法参数,不能重载返回类型。不能重载返回类型的函数。另一个选项是为扩展方法创建不同的静态类。如果它们在单独的类中,重载就不会成为问题。或者,您可以泛化
    Mesh
    类,并使用
    t[]
    作为返回类型?要了解为什么这不可能,如果使用
    var bob=Mesh.GetNGonCenters()
    ,您希望发生什么。它将如何决定调用哪个方法以及
    bob
    的类型?它不能,所以它不会让你这么做。当一个可能的返回类型是
    byte[]
    时,有没有办法做到这一点?它给出了错误“无效的约束类型。用作约束的类型必须是接口、非密封类或类型参数”。实际上,即使我的
    byte[]
    问题不存在,这个答案也不是很有用,因为您显然必须始终显式指定泛型类型参数。也就是说,OP必须调用
    GetNGonCenters()
    GetNGonCenters()
    ,在这种情况下,使用两个不同名称的方法更有意义。
    public static T[] GetNGonCenters<T>(this Mesh mesh) where T : Point3d
    {
        T[] centers = new T[mesh.Ngons.Count];
    
        for (int i = 0; i < mesh.Ngons.Count; i++)
            centers[i] = (T)mesh.Ngons.GetNgonCenter(i);
    
        return centers;
    }
    
    public class Foo
    {
        public int Number { get; set; }
    
        private void DoSomething(int num)
        {
            Number += num;
        }
    
        private int DoSomething(int num)
        {
            // Bad example, but still valid.
            Number = num + 2;
            return num * num;
        }
    }
    
    var foo = new Foo();
    
    // Which version of the method do I want to call here?
    // Most likely it is the one that returns void,
    // but you can ignore the return type of any method call.
    foo.DoSomething(3);