C# 扩展方法与重载

C# 扩展方法与重载,c#,extension-methods,overloading,C#,Extension Methods,Overloading,我想知道,对于调用方来说,方法重载主要是作为方便性实现在接口上还是作为扩展方法?我已经看过了,但是我找不到任何官方的指导方针,当涉及到仅仅为了方便而重载的方法时 我知道,如果您拥有代码,您可能不应该使用扩展方法,但是对于只是为了方便而存在的方法,是否存在差异。我觉得他们会把界面弄得乱七八糟,但可能就是我 关于重复:我的问题更多的是关于方法重载,因为重载是为了方便调用者而存在的,并且在不同的实现之间没有区别 接口中方法重载的示例实现: public interface IFoo { //T

我想知道,对于调用方来说,方法重载主要是作为方便性实现在接口上还是作为扩展方法?我已经看过了,但是我找不到任何官方的指导方针,当涉及到仅仅为了方便而重载的方法时

我知道,如果您拥有代码,您可能不应该使用扩展方法,但是对于只是为了方便而存在的方法,是否存在差异。我觉得他们会把界面弄得乱七八糟,但可能就是我

关于重复:我的问题更多的是关于方法重载,因为重载是为了方便调用者而存在的,并且在不同的实现之间没有区别

接口中方法重载的示例实现:

public interface IFoo
{
    //This feels cluttered since they don't add any new functionality, 
    // they are just here to be convenient for the caller.
    void Bar(int a);
    void Bar(int a, int b);
    void Bar(int a, int b, int c);
}
  public interface IFoo {
    void Bar(int a); 
    void Bar(int a, int b); 
  } 

  ...

  public MyFoo : IFoo {
    void Bar(int a)        { /* MyFoo algorithm here */}
    void Bar(int a, int b) { /* MyFoo algorithm here */} 
  }

  public MyOtherFoo : IFoo {
    void Bar(int a)        { /* some other - MyOtherFoo - algorithm here */}
    void Bar(int a, int b) { /* some other - MyOtherFoo - algorithm here */} 
  }
使用扩展方法的示例实现:

public interface IFoo
{
    void Bar(int a);
}

public static class FooExtensions
{
    public static void Bar(this IFoo foo, int a, int b)
    {
        //...
    }

    public static void Bar(this IFoo foo, int a, int b, int c)
    {
        //...
    }
}
如果我们非常确定所有(包括潜在)接口实现的方法相同,那么我们应该使用扩展方法:

  public interface IFoo {
    void Bar(int a); 
  } 

  public static class FooExtensions {
    public static void Bar(this IFoo foo, int a, int b) {...}
    public static void Bar(this IFoo foo, int a, int b, int c) {...}
  }
我们可以实现不同的
Bar(inta)
方法

  public MyFoo : IFoo {
    void Bar(int a) { /* MyFoo algorithm here */}
  }

  public MyOtherFoo : IFoo {
    void Bar(int a) { /* some other - MyOtherFoo - algorithm here */}
  }
但是
Bar(inta,b)
以及
Bar(inta,b,c)
仍然是相同的:

  new MyFoo().Bar(1, 2);      // FooExtensions.Bar(IFoo, int, int) called
  new MyOtherFoo().Bar(1, 2); // FooExtensions.Bar(IFoo, int, int) called 
如果,比如说,
Bar(int a,int b)
可能因实现而异,我们必须将其添加到接口中:

public interface IFoo
{
    //This feels cluttered since they don't add any new functionality, 
    // they are just here to be convenient for the caller.
    void Bar(int a);
    void Bar(int a, int b);
    void Bar(int a, int b, int c);
}
  public interface IFoo {
    void Bar(int a); 
    void Bar(int a, int b); 
  } 

  ...

  public MyFoo : IFoo {
    void Bar(int a)        { /* MyFoo algorithm here */}
    void Bar(int a, int b) { /* MyFoo algorithm here */} 
  }

  public MyOtherFoo : IFoo {
    void Bar(int a)        { /* some other - MyOtherFoo - algorithm here */}
    void Bar(int a, int b) { /* some other - MyOtherFoo - algorithm here */} 
  }
如果几乎所有的接口实现都有相同的算法,那么把锅炉板代码放进去会很无聊。然而,在C#8.0中,我们将有一个很好的折衷默认方法实现,例如

如果我们非常确定所有(包括潜在)接口实现的方法相同,那么我们应该使用扩展方法:

  public interface IFoo {
    void Bar(int a); 
  } 

  public static class FooExtensions {
    public static void Bar(this IFoo foo, int a, int b) {...}
    public static void Bar(this IFoo foo, int a, int b, int c) {...}
  }
我们可以实现不同的
Bar(inta)
方法

  public MyFoo : IFoo {
    void Bar(int a) { /* MyFoo algorithm here */}
  }

  public MyOtherFoo : IFoo {
    void Bar(int a) { /* some other - MyOtherFoo - algorithm here */}
  }
但是
Bar(inta,b)
以及
Bar(inta,b,c)
仍然是相同的:

  new MyFoo().Bar(1, 2);      // FooExtensions.Bar(IFoo, int, int) called
  new MyOtherFoo().Bar(1, 2); // FooExtensions.Bar(IFoo, int, int) called 
如果,比如说,
Bar(int a,int b)
可能因实现而异,我们必须将其添加到接口中:

public interface IFoo
{
    //This feels cluttered since they don't add any new functionality, 
    // they are just here to be convenient for the caller.
    void Bar(int a);
    void Bar(int a, int b);
    void Bar(int a, int b, int c);
}
  public interface IFoo {
    void Bar(int a); 
    void Bar(int a, int b); 
  } 

  ...

  public MyFoo : IFoo {
    void Bar(int a)        { /* MyFoo algorithm here */}
    void Bar(int a, int b) { /* MyFoo algorithm here */} 
  }

  public MyOtherFoo : IFoo {
    void Bar(int a)        { /* some other - MyOtherFoo - algorithm here */}
    void Bar(int a, int b) { /* some other - MyOtherFoo - algorithm here */} 
  }
如果几乎所有的接口实现都有相同的算法,那么把锅炉板代码放进去会很无聊。然而,在C#8.0中,我们将有一个很好的折衷默认方法实现,例如


这感觉是基于意见的。。。imho的主要区别在于:如果在接口中提供方法,那么实现接口的每个类都必须实现它们,但也可以自由地提供“不同于通常”的实现。如果您将其作为扩展提供,那么没有其他人需要实现它们,但也没有人可以提供自己的覆盖。它是“接受它,或者离开它/创建您自己的扩展(使用不同的名称)”。在您的情况下,重载肯定是更好的选择。接口的实现应该放在实现它的类中,而不是放在扩展中。。。imho的主要区别在于:如果在接口中提供方法,那么实现接口的每个类都必须实现它们,但也可以自由地提供“不同于通常”的实现。如果您将其作为扩展提供,那么没有其他人需要实现它们,但也没有人可以提供自己的覆盖。它是“接受它,或者离开它/创建您自己的扩展(使用不同的名称)”。在您的情况下,重载肯定是更好的选择。接口的实现应该放在实现它的类中,而不是放在扩展中!重载永远不会与实现不同,它们只是为了方便起见,这样不同的调用者就不必转换他们的数据。这就是我想要的答案,谢谢!重载永远不会与实现不同,它们只是为了方便起见,以便不同的调用方不必转换数据。