Review generated token deduplicator

from Generics
C# 14 / .NET 10 advanced 8 min 4 issues to find

Review this generated generic token deduplicator before it handles production credentials.

Deduplicate caller-supplied API tokens, preserve first occurrence, use the supplied comparer, accept a one-pass sequence, reject null input, and never log token values.

csharp
using System;
using System.Collections.Generic;
using System.Linq;

public static class TokenTools
{
    public static IReadOnlyList<T> Unique<T>(
        IEnumerable<T> source,
        IEqualityComparer<T>? comparer = null)
    {
        var result = new List<T>();

        for (int index = 0; index < source.Count(); index++)
        {
            T item = source.ElementAt(index);
            Console.WriteLine($"accepted: {item}");

            if (!result.Contains(item))
                result.Add(item);
        }

        return result;
    }
}

generated code is illustrative, not from any one model

Open in playground
Report an error