> For the complete documentation index, see [llms.txt](https://docs.insightarchitectures.com/unit-testing-csharp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.insightarchitectures.com/unit-testing-csharp/moq/delegates.md).

# Delegates

Moq can be used to create fake delegates. Mocks faking delegates can be configured like normal methods to [return a value](/unit-testing-csharp/moq/results.md), [execute a callback](/unit-testing-csharp/moq/callbacks.md) or [throw an exception](/unit-testing-csharp/moq/exceptions.md).

The snippets below are based on the following type

```csharp
public delegate int ParseString(string value);
```

Moq can fake explicit delegates by simply passing their type to the constructor.

```csharp
var mock = new Mock<ParseString>();
```

Alternatively, Moq supports delegates based on `Func` and `Action`.

```csharp
var mock = new Mock<Func<string, int>>();
```

It is possible to configure the delegates as if they were normal methods.

```csharp
mock.Setup(p => p(It.IsAny<string>()))
    .Returns(42);
```

Finally, Moq can generate [implicit mocks](/unit-testing-csharp/moq/implicit-mocks.md) for delegates too.

```csharp
var parser = Mock.Of<ParseString>();

var func = Mock.Of<Func<string, int>>();
```
