Using Generic Constraints & Default

Here is a great video from one of my buddies, Jeremy Clark, on generic constraints and default:

Here is an extension method I made from Jeremy’s second example:

public static T Max<T>(this T obj1, T obj2) where T : IComparable
{
    if obj1.CompareTo(obj2) >= 0 ? return obj1 : return obj2;
}

Discover more from dotNetTips.com

Subscribe to get the latest posts sent to your email.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.