How it works
A generic method works with a type parameter rather than one fixed concrete type. That does not mean it can perform every possible operation on that type. Minimum needs a way to compare two values. The IComparable<T> constraint states that requirement explicitly, so calling CompareTo is valid inside the method.
The integer call chooses four because nine compares greater than four. The string call chooses alpha for these simple lowercase words under the comparison used by string.CompareTo. Both invocations share the same method body, while each value type supplies the comparison operation promised by its interface implementation.
A constraint is a capability contract, not a guarantee that all implementations have identical domain meaning. String comparison can be culture-sensitive. A business identifier may instead require an explicit ordinal comparer. A reusable minimum operation that needs caller-selected ordering could accept IComparer<T> rather than hard-code the value type's default comparison.
The expression uses less than or equal to zero, so equal values return the left argument. That tie rule is observable if two equal-comparing objects contain different additional information. Decide whether retaining the first candidate is part of the contract and test it; comparison equality does not necessarily mean reference identity.
Practice by defining a small Score type implementing IComparable<Score> and compare scores with the generic method. Include smaller, larger, and equal scores. Then refactor a second overload to accept an IComparer<T>, and use StringComparer.OrdinalIgnoreCase for identifier-like strings.
Avoid adding constraints simply because they are available. Each one limits which callers can use the method. Require only the capabilities the implementation actually needs, and separate ordering, equality, construction, and ownership into their own decisions. A precise generic contract is easier to reuse than one that assumes an entire unrelated class hierarchy.
A working example
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
Console.WriteLine(Minimum(9, 4));
Console.WriteLine(Minimum("beta", "alpha"));
static T Minimum<T>(T left, T right) where T : IComparable<T> {
return left.CompareTo(right) <= 0 ? left : right;
}
Expected output
4 alpha
A constraint lets the generic method call a capability guaranteed for T.