# 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>>();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.educationsmediagroup.com/unit-testing-csharp/moq/delegates.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
