CHAPTER 11 ADVANCED C# LANGUAGE FEATURES
Note Understand that a given extension method can have multiple parameters, but only the first parameter can
be qualified with this. The additional parameters would be treated as normal incoming parameters for use by the
method.
Invoking Extension Methods
Now that we have these extension methods in place, consider the following Main() method:
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Extension Methods *****\n");
// The int has assumed a new identity!
int myInt = 12345678;
myInt.DisplayDefiningAssembly();
// So has the DataSet!
System.Data.DataSet d = new System.Data.DataSet();
d.DisplayDefiningAssembly();
// And the SoundPlayer!
System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
sp.DisplayDefiningAssembly();
// Use new integer functionality.
Console.WriteLine("Value of myInt: {0}", myInt);
Console.WriteLine("Reversed digits of myInt: {0}", myInt.ReverseDigits());
}
Console.ReadLine();
Here is the output (minus the beeping):
***** Fun with Extension Methods *****
Int32 lives here: => mscorlib
DataSet lives here: => System.Data
SoundPlayer lives here: => System
Value of myInt: 12345678
Reversed digits of myInt: 87654321
420