使用委托代替接口

发布于 2024-11-06 21:44:54 字数 150 浏览 0 评论 0原文

我读到您可以使用接口和委托来达到相同的目的。就像,您可以使用委托而不是接口。

有人可以举个例子吗?我在《nutshell》书中看到过一个例子,但我不记得了,想问问。

能否提供一些示例代码?用例?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺孤伤于心 2024-11-13 21:44:54

如果您的接口只有一个方法,那么使用委托会更方便。

比较以下示例:

使用接口

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));
}

使用委托

// 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));
}

您可以看到委托版本稍小一些。

使用匿名方法和“Func”委托

如果将其与内置 .NET 通用委托(FuncAction 等)和匿名方法结合使用方法,您可以将整个代码替换为:

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));
}

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

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:

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));
}
猥︴琐丶欲为 2024-11-13 21:44:54

委托的使用方式与单方法接口相同:

interface ICommand
 {
   void Execute();
 }

delegate void Command();

Delegates can be used in the same way as a single-method interface:

interface ICommand
 {
   void Execute();
 }

delegate void Command();
只为守护你 2024-11-13 21:44:54

当您使用委托时:

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
    }
}

当您使用接口时:

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
    }
}

用例 - 提供只能执行一个操作的事物。委托很好,因为您不必创建新类,您只需采用具有相同签名的方法即可,甚至传递一个调用具有不同签名的方法的 lambda。但如果您决定获得更复杂的逻辑,接口会更灵活:

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
    }
}

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. 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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文