C# generic extension method with type contraint

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		int number = 5;
		var IsDefault = number.IsDefault();
		Console.WriteLine(IsDefault);
	}
}

static class Extensions
{
	public static bool IsDefault<T>(this T obj)where T : struct, IEquatable<T>
	{
		return EqualityComparer<T>.Default.Equals(obj, default (T));
	}
}
;