How Moq It.IsAny <T> () is Implemented
Taking the base into the GitHub source code , what really matches is a given predicate of the type Predicate<T> (or Func<T, bool> , if you like):
value => value == null || typeof(TValue).IsAssignableFrom(value.GetType()) It checks if value is null, or if value is the same type as the TValue argument for IsAny<TValue> using Type.IsAssignableFrom .
It then returns the default(T) value of this type to allow the funny method to match the signature correctly. And, as DavidH pointed out, you cannot use null for value types, so using a default value (T) is preferred as a general return value.
The iamba that we use when we Setup mocks in Moq are not ordinary delegates, they are expression trees . The It.Is<> method is just a place. This is not necessarily called. It doesn't matter if it returns default(T) or just throws a NotSupportedException or something else. Let me give an example:
interface IMyFace { void MethodToTest(object obj); } static class Test { public static void Main() { Setup<IMyFace>(x => x.MethodToTest(null)); Setup<IMyFace>(x => x.MethodToTest(MyAnyA<object>())); Setup<IMyFace>(x => x.MethodToTest(MyAnyB<object>())); } public static TArg MyAnyA<TArg>() { // never runs return default(TArg); } public static TArg MyAnyB<TArg>() { // never runs return default(TArg); } public static void Setup<T>(Expression<Action<T>> expr) where T : class { Console.WriteLine("The expr was: " + expr); } } Not very interesting, but it should show that with Expression<...> expression trees you can see which method was used, not just the return value. Learn more about exploring the expression tree on MSDN, expression trees .