Question: I have a class that requires a way to retrieve a random integer value with a maximum. I don't want this class to depend on a specific way to retrieve that random value (such as system.random). Would it be best to:

(A) Use a public **delegate** (or func)

```
public delegate int NextRandomInt(int maxValue);

public class MyClass
{
    public NextRandomInt NextRandomInt { get; set; }

    public MyClass(NextRandomInt nextRandomInt)
    {
        NextRandomInt = nextRandomInt;
    }
}

```

(B) Use a public **interface**

```
public interface IRandomIntProvider
{
    int NextInt(int maxValue);
}

public class MyClass
{
    public IRandomIntProvider RandomIntProvider { get; set; }

    public MyClass(IRandomIntProvider randomIntProvider)
    {
        RandomIntProvider = randomIntProvider;
    }
}

```

(C) Something else all together.

Both ways work. I feel like using a delegate would be simpler and quicker to implement, but the interface is more readable and may be easier when dependency injection comes around.

Answer: 
It depends as to how much job you want to implement using a delegate or an interface.

If your interface is going to have only one or even two methods, you could use a `Func` to force the same behavior. Otherwise i would use `interface`s.

`Mark Seemann` explained just this pretty nicely here : [link](http://blog.ploeh.dk/2009/05/28/DelegatesAreAnonymousInterfaces/)

To sum it up he states this:

> 
> Whenever possible, I prefer to model my APIs with delegates instead of one-method interfaces, since it gives me greater flexibility and less infrastructure code.
> 
> 
> Obviously, this technique only works as long as you only need to abstract a single method. As soon as your abstraction needs a second method, you will need to introduce a proper interface or, preferably, an abstract base class.
> 
> 
>
1. Use Interface if you think that there may be another implementation of `NextRandomInt()` may exist in the system.
2. Choose delegate if you think it shall be executed as a cause of an event or callback.  

Otherwise simply go with a method as class member. It's up to you if you want the method to be available as `instance` method or `static`.