Developer Tools

C# Class Generator

List the properties of a type and get modern C#: a sealed class, record or record struct with auto-properties, required members for non-nullable references, init-only setters, nullable reference types, an optional constructor or positional record, System.Text.Json attributes and XML documentation, in a file-scoped namespace.

  • Runs in your browser
  • No sign-up
  • Free to use
Start from an example

Types: string, int, float (double), bool, date (DateTimeOffset), any (object), a class name, T[] (List) or map<K, V> (Dictionary). name? = nullable.

Options

    How to use C# Class Generator

    1. Enter the class name and namespace.
    2. Choose class, record or record struct.
    3. List the properties as name: type = default.
    4. Pick the options and copy the code.

    C# Class Generator features

    Classes and records

    sealed class, record, record struct or positional record.

    Required members

    C# 11 required for non-nullable properties.

    Immutability

    init-only setters and readonly record structs.

    Nullable types

    string? for optional members with #nullable enable.

    JSON attributes

    [JsonPropertyName] with camelCase names.

    Defaults

    Values, DateTimeOffset.UtcNow and collection expressions.

    When to use C# Class Generator

    • Creating DTOs for ASP.NET Core APIs.
    • Defining event and message records.
    • Modelling configuration bound from appsettings.json.
    • Writing entity classes for Entity Framework Core.

    C# Class Generator FAQ

    Class or record?

    Records have value equality and with-expressions, which suits immutable data such as DTOs and events. Classes have reference equality and suit entities with identity and changing state.

    What does required do?

    Since C# 11, required properties must be set in every object initialiser; the compiler reports a missing one. It complements nullable reference types.

    What are init-only setters?

    Properties with init can be set during construction and in with-expressions, but not afterwards.

    Why are names converted to PascalCase?

    C# properties use PascalCase by convention. With the JSON option, JsonPropertyName keeps camelCase names in the JSON.

    Which C# version is needed?

    File-scoped namespaces need C# 10, required members C# 11 and the [] collection expression C# 12. The notes mention what each feature requires.

    Is anything uploaded?

    No. The code is generated in your browser.

    Modern C# data types

    C# has gained many features for data-centric types in recent versions: records with value equality, init-only setters, required members, nullable reference types, file-scoped namespaces and collection expressions. Combining them correctly is what makes a type safe to use, and the generator does it from a short property list.

    Properties use neutral types that map to C#: string, int, double, bool, DateTimeOffset, object, List<T> and Dictionary<K, V>. A question mark after the name makes the property nullable. Names are converted to PascalCase, and with the JSON option each property gets a JsonPropertyName attribute with the camelCase name used in JSON.

    Non-nullable reference properties without a default are where bugs come from: an object can be created without them. Marking them required makes the compiler enforce that every object initialiser sets them, which works together with #nullable enable to eliminate a whole class of null reference exceptions.

    Records are the natural choice for data that does not change. A positional record declares its properties in the primary constructor and gets equality, ToString and deconstruction for free; a record struct avoids heap allocation for small values. init-only setters make classes and records immutable after construction while still allowing object initialisers and with-expressions.

    Defaults are written in the way C# allows: literals for simple values, DateTimeOffset.UtcNow for timestamps and [] collection expressions for lists. Classes are sealed by default, which is a recommended practice for types that are not designed for inheritance.

    Other useful tools