---
title: Collections
description: Create arrays, lists, and sequences, assign collection properties with With, and populate existing collections with AddManyTo.
---

AutoFixture can produce standalone collections (`Create<string[]>()`, `CreateMany<int>(5)`), assign a collection to a writable property with `With`, or append items to an `ICollection<T>` you already hold with `AddManyTo`.

## Example scenario

You are testing code that sums a list of integers, reads tags from a writable list property, or needs an order with populated lines before your service accepts it.

## Create arrays and lists

`Create` fills arrays and lists with anonymous elements:

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

var tags = fixture.Create<string[]>();
var scores = fixture.Create<List<int>>();

Assert.NotEmpty(tags);
Assert.NotEmpty(scores);
Assert.All(tags, tag => Assert.False(string.IsNullOrEmpty(tag)));
```

AutoFixture picks a default length based on [`RepeatCount`](/api/autofixture/5-0-0-rc-1/autofixture.fixture#repeatcount).

## CreateMany

When you need a specific number of distinct values, use `CreateMany<T>(count)`:

```csharp
var values = fixture.CreateMany<int>(5).ToList();

Assert.Equal(5, values.Count);
Assert.True(values.Distinct().Count() > 1);
```

`CreateMany` returns a lazy `IEnumerable<T>` — call `.ToList()` or `.ToArray()` when you need a fixed collection. Values are distinct by default for primitive types. See also [Fixture and Create](/docs/fundamentals/fixture-and-create).

The [Seed extensions](/docs/extensions/seed-extensions) package adds seeded overloads such as `CreateMany("Item", 3)` for readable string prefixes.

## Assign collection properties with With

When a property is assignable, create the collection and assign it with `With`:

```csharp
public sealed class TagBag
{
    public List<string> Labels { get; set; } = [];
}
```

```csharp
var tags = fixture.Build<TagBag>()
    .With(b => b.Labels, fixture.CreateMany<string>().ToList())
    .Create();

Assert.Equal(fixture.RepeatCount, tags.Labels.Count);
```

For arrays:

```csharp
public sealed class ScoreBatch
{
    public int[] Values { get; set; } = [];
}
```

```csharp
var batch = fixture.Build<ScoreBatch>()
    .With(b => b.Values, fixture.CreateMany<int>().ToArray())
    .Create();

Assert.Equal(fixture.RepeatCount, batch.Values.Length);
```

## AddManyTo on an existing collection

[`AddManyTo`](/api/autofixture/5-0-0-rc-1/autofixture.collectionfiller) appends anonymous items to an `ICollection<T>` you already have. By default it adds [`RepeatCount`](/api/autofixture/5-0-0-rc-1/autofixture.fixture#repeatcount) items:

```csharp
var lines = new List<OrderLine>();
fixture.AddManyTo(lines);

Assert.Equal(fixture.RepeatCount, lines.Count);
```

Pass an explicit count or factory when you need control:

```csharp
fixture.AddManyTo(lines, 3);

fixture.AddManyTo(lines, () => fixture.Build<OrderLine>()
    .With(l => l.Quantity, 1u)
    .Create());
```

## Read-only collection properties

Some types expose a collection through a read-only property — the list is created in the constructor and cannot be replaced with `With`. Plain `Create<Order>()` leaves `OrderLines` empty unless you enable [`ReadonlyCollectionPropertiesBehavior`](/api/autofixture/5-0-0-rc-1/autofixture.readonlycollectionpropertiesbehavior):

```csharp
var fixture = new Fixture();
fixture.Behaviors.Add(new ReadonlyCollectionPropertiesBehavior());

var order = fixture.Create<Order>();

Assert.Equal(fixture.RepeatCount, order.OrderLines.Count);
```

The behavior calls `Add` on read-only properties that implement `ICollection<T>`. Item count follows [`RepeatCount`](/api/autofixture/5-0-0-rc-1/autofixture.fixture#repeatcount).

For a single order without enabling the behavior globally:

```csharp
var order = fixture.Build<Order>()
    .Do(o => fixture.AddManyTo(o.OrderLines))
    .Create();

Assert.Equal(fixture.RepeatCount, order.OrderLines.Count);
```

To add one specific item, use `Do` — see [Refactoring a test](/docs/get-started/refactoring-a-test).

## How it works

- **`Create<T[]>()` / `Create<List<T>>()`** — standalone collections filled in one call
- **`CreateMany<T>(count)`** — lazy sequence of distinct specimens
- **`With(..., CreateMany<T>().ToList())`** — assigns a populated collection during the build
- **`AddManyTo(collection)`** — appends items to an existing `ICollection<T>`
- **`ReadonlyCollectionPropertiesBehavior`** — fills read-only `ICollection<T>` properties via `Add`
- **`Build` + `Do` + `AddManyTo`** — populate one specimen's read-only collection without a global behavior

When nested graphs fail validation, combine these with [Specimen graphs](/docs/fundamentals/specimen-graphs) and [Build DSL](/docs/fundamentals/build-dsl).

## Next steps

- [Specimen graphs](/docs/fundamentals/specimen-graphs)
- [Build DSL](/docs/fundamentals/build-dsl)
- [Data annotations](/docs/how-to/data-annotations)
- [Refactoring a test](/docs/get-started/refactoring-a-test)
- [Seed extensions](/docs/extensions/seed-extensions)

## API

- [`ReadonlyCollectionPropertiesBehavior`](/api/autofixture/5-0-0-rc-1/autofixture.readonlycollectionpropertiesbehavior)
- [`CollectionFiller.AddManyTo`](/api/autofixture/5-0-0-rc-1/autofixture.collectionfiller)
- [`Fixture.RepeatCount`](/api/autofixture/5-0-0-rc-1/autofixture.fixture#repeatcount)
- [`CreateMany` extension](/api/autofixture/5-0-0-rc-1/autofixture.specimenfactory#createmany)
