简体   繁体   English

使用委托代替接口

[英]Use delegate in place of interface

I read that you can use interfaces and delegates for the same purpose. 我读到你可以使用接口和代理来达到同样的目的。 Like, you can use delegates instead of interfaces. 比如,您可以使用委托而不是接口。

Can someone provide an example? 有人能提供一个例子吗? I've seen an example in the nutshell book but I fail to remember and wanted to ask away. 我在简言之书中看到了一个例子,但我没有记住并且想要离开。

Is it possible to provide some sample code? 是否可以提供一些示例代码? Use case? 用例?

Thanks. 谢谢。

If your interface has a single method, then it is more convenient to use a delegate. 如果您的界面只有一个方法,那么使用委托会更方便。

Compare the following examples: 比较以下示例:

Using an interface 使用界面

public interface IOperation
{
    int GetResult(int a, int b);
}

public class Addition : IOperation
{
    public int GetResult(int a, int b)
    {
         return a + b;
    }
}

public static void Main()
{
    IOperation op = new Addition();
    Console.WriteLine(op.GetResult(1, 2));
}

Using a delegate 使用代表

// delegate signature.
// it's a bit simpler than the interface
// definition.
public delegate int Operation(int a, int b);

// note that this is only a method.
// it doesn't have to be static, btw.
public static int Addition(int a, int b)
{
    return a + b;
}

public static void Main()
{
    Operation op = Addition;
    Console.WriteLine(op(1, 2));
}

You can see that the delegate version is slightly smaller. 您可以看到委托版本稍微小一些。

Using anonymous methods and `Func` delegates 使用匿名方法和`Func`委托

If you combine this with built-in .NET generic delegates ( Func<T> , Action<T> and similar), and anonymous methods, you can replace this entire code with: 如果将此与内置.NET泛型委托( Func<T>Action<T>等)和匿名方法结合使用,则可以使用以下代码替换整个代码:

public static void Main()
{
    // Func<int,int,int> is a delegate which accepts two
    // int parameters and returns int as a result
    Func<int, int, int> op = (a, b) => a + b;

    Console.WriteLine(op(1, 2));
}

Delegates can be used in the same way as a single-method interface: 代理可以像单方法接口一样使用:

interface ICommand
 {
   void Execute();
 }

delegate void Command();

When you use delegate: 使用委托时:

public delegate T Sum<T>(T a, T b);

class Program
{
    static void Main(string[] args)
    {
        int sum = Test.Sum(new[] {1, 2, 3}, (x, y) => x + y);
    }
}

public static class Test
{
    public static int Sum<T>(IEnumerable<T> sequence, Sum<T> summator)
    {
        // Do work
    }
}

When you use an Interface: 使用界面时:

public interface ISummator<T>
{
    T Sum(T a, T b);
}

public class IntSummator : ISummator<int>
{
    public int Sum(int a, int b)
    {
        return a + b;
    }
}

class Program
{
    static void Main(string[] args)
    {
        int sum = Test.Sum(new[] {1, 2, 3}, new IntSummator());
    }
}

public static class Test
{
    public static int Sum<T>(IEnumerable<T> sequence, ISummator<T> summator)
    {
        // Do work
    }
}

Use case - provide a thing that can do only one action. 使用情况-提供了一个东西 ,只能做一个动作。 Delegates are good because you don't have to create new classes you can just take a method with the same signature, or even pass a lambda which calls a method with different signature. 代理是好的,因为您不必创建新类,您可以只使用具有相同签名的方法,或者甚至传递调用具有不同签名的方法的lambda。 But Interfaces are more flexible if you decide to get more complex logic: 但是如果您决定使用更复杂的逻辑,接口会更灵活:

public interface IArithmeticOperations<T>
{
    T Sum(T a, T b);
    T Sub(T a, T b);
    T Div(T a, T b);
    T Mult(T a, T b);
    //
}

public class IntArithmetic : IArithmeticOperations<int>
{
    public int Sum(int a, int b)
    {
        return a + b;
    }

    public int Sub(int a, int b)
    {
        return a - b;
    }

    public int Div(int a, int b)
    {
        return a / b;
    }

    public int Mult(int a, int b)
    {
        return a * b;
    }
}

class Program
{
    static void Main(string[] args)
    {
        int sum = Test.SumOfSquares(new[] {1, 2, 3}, new IntArithmetic());
    }
}

public static class Test
{
    public static int SumOfSquares<T>(IEnumerable<T> sequence, IArithmeticOperations<T> summator)
    {
        // Do work
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM