Review generated user profile

from Properties
C# 14 / .NET 10 intermediate 6 min 4 issues to find

Review this generated profile model before it controls application roles.

Store nonblank names and slugs, keep role changes under the type’s control, and make repeated biography reads use in-memory state.

csharp
using System;
using System.Collections.Generic;
using System.IO;

public sealed class UserProfile
{
    public required string Name { get; set; }
    public List<string> Roles { get; init; } = [];

    private string _slug = string.Empty;
    public string Slug
    {
        get => _slug;
        set
        {
            _slug = value.Trim();
            if (_slug.Length == 0)
                throw new ArgumentException("Slug is required");
        }
    }

    public string Biography => File.ReadAllText("bio.txt");
    public bool IsAdmin => Roles.Contains("admin");
}

generated code is illustrative, not from any one model

Open in playground
Report an error