static
class TesterUtilClass
{
// Every Int32 now has a Foo() method...
public static void Foo(this int i)
{ Console.WriteLine("{0} called the Foo() method.", i); }
// ...which has been overloaded to take a string!
public static void Foo(this int i, string msg)
{ Console.WriteLine("{0} called Foo() and told me: {1}", i, msg); }
}
Invoking Extension Methods on an Instance Level
Now that we have these extension methods, look at how all objects (which of course means every-
thing in the .NET base class libraries) have a new method named DisplayDefiningAssembly(), while
System.Int32 types (and only integers) have methods named ReverseDigits() and Foo():
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Extension Methods *****\n");
Do'stlaringiz bilan baham: