Is there any practical difference between an extension method on <T> or on Object?
Is there any practical difference between these two extension methods?
class Extensions
{
public static void Foo<T>(this T obj) where T : class { ... }
public static 开发者_运维知识库void Foo(this object obj) { ... }
}
I was poking around in Extension Overflow and I came across the first form, which I haven't used before. Curious what the difference is.
Extension methods on Object
will also apply to value types. (And they'll be boxed by the call, reducing performance)
Extension methods on <T>
but without where T : class
will also work on value types, but will not box them.
In addition, extension methods on <T>
can write typeof(T)
to get the compile-time type of their invocation.
If you do that, note the difference between
someButton.Extension();
someButton.Extension<Control>();
someButton.Extension<Object>();
精彩评论