Free mag vol1 | Page 413

CHAPTER 9  COLLECTIONS AND GENERICS // the method does not take params. DisplayBaseClass(); DisplayBaseClass(); } // Compiler error! No params? Must supply placeholder! // DisplayBaseClass(); Console.ReadLine(); Currently, the generic Swap and DisplayBaseClass methods are defined within the application’s Program class. Of course, as with any method, you are free to define these members in a separate class type (MyGenericMethods) if you would prefer to do it that way: public static class MyGenericMethods { public static void Swap(ref T a, ref T b) { Console.WriteLine("You sent the Swap() method a {0}", typeof(T)); T temp; temp = a; a = b; b = temp; } } public static void DisplayBaseClass() { Console.WriteLine("Base class of {0} is: {1}.", typeof(T), typeof(T).BaseType); } The static Swap and DisplayBaseClass methods have been scoped within a new static class type, so you need to specify the type’s name when invoking either member, as in this example: MyGenericMethods.Swap(ref a, ref b); Of course, generic methods do not need to be static. If Swap and DisplayBaseClass were instance level (and defined in a nonstatic class), you would simply make an instance of MyGenericMethods and invoke them using the object variable: MyGenericMethods c = new MyGenericMethods(); c.Swap(ref a, ref b);  Source Code You can find the CustomGenericMethods project under the Chapter 9 subdirectory. 352