ZetCode

C# Bogus

last modified October 5, 2026

In this article, we show how to generate fake data in C# using the Bogus library.

Bogus is a fake data generator for .NET. It provides both a fluent API for mapping generated values to an object and a procedural API for individual values.

Fake data is useful when developing and testing applications because it lets us work with realistic-looking values without using real personal information.

We add Bogus to a project with the .NET CLI.

$ dotnet add package Bogus

The command adds the Bogus NuGet package and makes its types available to the project.

C# Bogus simple example

The following example generates five users with a full name and an occupation.

Program.cs
using Bogus;

var occupations = new[] { "gardener", "teacher", "writer", "programmer" };
var userFaker = new Faker<User>()
    .RuleFor(u => u.Name, f => f.Name.FullName())
    .RuleFor(u => u.Occupation, f => f.PickRandom(occupations));

var users = userFaker.Generate(5);
Console.WriteLine(string.Join("\n", users));

public class User
{
    public string Name { get; set; } = "";
    public string Occupation { get; set; } = "";

    public override string ToString() => $"{Name} ({Occupation})";
}

The Faker<User> object contains rules for the two properties. The Name.FullName method creates a name, while PickRandom selects one occupation from the array.

Generate creates the requested number of objects. Since Bogus uses random data, the output differs between runs.

$ dotnet run
Donny Torphy (gardener)
Ansel Hauck (teacher)
Edyth Friesen (programmer)
Constantin Howe (programmer)
Dayana Gorczany (teacher)

C# Bogus procedural syntax

The procedural API is convenient when we need a few values rather than a complete object graph.

Program.cs
using Bogus;

var faker = new Faker();

var firstName = faker.Person.FirstName;
var lastName = faker.Person.LastName;
var email = faker.Person.Email;

Console.WriteLine($"{firstName} {lastName} {email}");

The Person property exposes related values for a generated person, including a first name, a last name, and an email address.

$ dotnet run
Connie Trantow Connie14@hotmail.com

C# Bogus localized data

A locale passed to the Faker constructor selects the locale used by the data sets. The following example generates Slovak and Russian names.

Program.cs
using Bogus;

var slovakFaker = new Faker("sk");
Console.WriteLine($"{slovakFaker.Person.FirstName} {slovakFaker.Person.LastName}");

var russianFaker = new Faker("ru");
Console.WriteLine($"{russianFaker.Person.FirstName} {russianFaker.Person.LastName}");
$ dotnet run
Matej Farkašovský
Валентин Кузьмин

C# Bogus rules by type

RuleForType applies a rule to members of a specified type. A property-specific rule can then override the general rule.

Program.cs
using Bogus;

var itemFaker = new Faker<Item>()
    .RuleForType(typeof(string), f => f.Lorem.Word())
    .RuleForType(typeof(int), f => f.Random.Int(1, 100))
    .RuleFor(i => i.Weight, f => f.Random.Double(100, 200));

var items = itemFaker.Generate(3);
Console.WriteLine(string.Join("\n", items));

public class Item
{
    public string Name { get; set; } = "";
    public int Quantity { get; set; }
    public double Weight { get; set; }

    public override string ToString() => $"{Name} {Quantity} {Weight:F2}";
}

The first two rules fill string and integer members. The final rule targets the Weight property specifically and generates a value from 100 through 200.

$ dotnet run
maxime 42 199.63
dolore 17 189.22
tempora 81 111.86

C# Bogus dates

The Date property provides methods for generating dates in a range, including dates in the future, in the past, or between two specified values.

Program.cs
using Bogus;

var faker = new Faker();
Console.WriteLine(faker.Date.Future(20));
Console.WriteLine(faker.Date.Past(50));
Console.WriteLine(faker.Date.Between(new DateTime(2022, 1, 1),
    new DateTime(2025, 1, 1)));

Future generates a date no more than the specified number of years in the future, while Past does the same for the past. The Between method returns a date between its two arguments.

$ dotnet run
1/16/2027 6:56:03 PM
1/29/1979 8:21:46 AM
1/12/2023 6:54:39 PM

Source

Bogus on GitHub

Bogus on NuGet

In this article, we used Bogus to generate fake names, occupations, localized data, numbers, and dates in C#.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all C# tutorials.