Use delegate instead of interface

I read that you can use interfaces and delegates for the same purpose. For example, you can use delegates instead of interfaces.

Can anyone provide an example? I saw the example in a nutshell, but I don’t remember and wanted to apologize.

Can I provide some sample code? Use case?

Thank.

+5
source share
3 answers

If your interface has one method, it is more convenient to use a delegate.

Compare the following examples:

Using 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 Delegate

// delegate signature.
// it 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 delegates of `Func`

.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));
}
+8

, :

interface ICommand
 {
   void Execute();
 }

delegate void Command();
+2

:

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

- , . , , , . , :

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

All Articles