> 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/generic-methods.md).

# Generic methods

Generic methods require type parameters to be specified to be invoked. When dealing with dependencies exposing generic methods, developers can use Moq to configure these methods accordingly by placing constraints on the incoming type parameters.

The snippets in this section will be based on the interface below:

```csharp
public interface IService
{
    void DoSomething<T>(T argument);
}
```

## Expecting any type

Like for configuring methods by accepting any parameters, methods can be configured to accept any type. This can be done with the constant `It.IsAnyType`.

```csharp
mock.Setup(p => p.DoSomething(It.IsAny<It.IsAnyType>()))
    .Callback((object value) => Debug.WriteLine($"DoSomething: {value}"));
```

## Setting expectations on incoming type parameter

Sometimes, the configuration needs to be more specific than what is allowed from the compiler. In this case, developers can use `It.IsSubType<T>` to constrain the incoming type to the same type or any of its subtypes. This allows different setups for different incoming types

```csharp
mock.Setup(p => p.DoSomething(It.IsAny<It.IsSubtype<IList<string>>>()))
    .Callback((IList<string> items) => Debug.WriteLine($"Received list of {items.Count} strings"));

mock.Setup(p => p.DoSomething(It.IsAny<It.IsSubtype<IList<int>>>()))
    .Throws<ArgumentException>();
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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.insightarchitectures.com/unit-testing-csharp/moq/generic-methods.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.
