---
title: Behaviors
description: Control recursion, constructor choice, and specimen policies with behaviors and attributes.
---

Behaviors change how AutoFixture handles edge cases like circular graphs and multi-constructor types.

## Example scenario

You have a `TreeNode` type where each node references a parent, causing circular graphs. You also have `MultiCtorProduct` with both a parameterless and a greedy constructor — and you need to pick which one AutoFixture uses in xUnit.net 3 or NUnit 4 theories.

## Example

**Recursion** — default `ThrowingRecursionBehavior` throws on circular graphs:

```csharp
var fixture = new Fixture();

Assert.ThrowsAny<ObjectCreationException>(() => fixture.Create<TreeNode>());
```

Switch to `OmitOnRecursionBehavior` to omit recursive properties instead. See [Circular references](/docs/how-to/circular-refs).

### Modest and Greedy

Pick constructor greediness per parameter in theories (same attributes exist for [xUnit.net 3](/docs/integrations/xunit3) and [NUnit 4](/docs/integrations/nunit4)):

```csharp
[Theory, AutoData]
public void ModestAttribute_UsesParameterlessConstructor([Modest] MultiCtorProduct product)
{
    Assert.Equal("default", product.Name);
    Assert.Equal(0m, product.Price);
}

[Theory, AutoData]
public void GreedyAttribute_UsesFullestConstructor([Greedy] MultiCtorProduct product)
{
    Assert.False(string.IsNullOrEmpty(product.Name));
    Assert.NotEqual(0m, product.Price);
}
```

## How it works

- **Behaviors** — global policies on the fixture (recursion, omitting properties, etc.)
- **Modest** — prefers the constructor with fewest parameters
- **Greedy** — prefers the constructor with most parameters

Requires [xUnit.net 3](/docs/integrations/xunit3) or [NUnit 4](/docs/integrations/nunit4) for `Modest` and `Greedy` attributes.

## Next steps

- [Circular references](/docs/how-to/circular-refs)
- [xUnit.net 3](/docs/integrations/xunit3)
- [NUnit 4](/docs/integrations/nunit4)

## API

- [`ThrowingRecursionBehavior`](/api/autofixture/5-0-0-rc-1/autofixture.throwingrecursionbehavior)
- [`OmitOnRecursionBehavior`](/api/autofixture/5-0-0-rc-1/autofixture.omitonrecursionbehavior)
- [`ModestAttribute`](/api/xunit3/5-0-0-rc-1/autofixture.xunit3.modestattribute) (also on [NUnit 4](/api/nunit4/5-0-0-rc-1/autofixture.nunit4.modestattribute))
- [`GreedyAttribute`](/api/xunit3/5-0-0-rc-1/autofixture.xunit3.greedyattribute) (also on [NUnit 4](/api/nunit4/5-0-0-rc-1/autofixture.nunit4.greedyattribute))
