We have generic methods in .Net2.0. The generic methods can be associated to any class.


 


Demonstrate: Generic Method


 


public class GenericMethodDemo


{


public static void Copy<T> (List<T> source, List<T> destination)


{


    foreach (T obj in source)


      {


    destination.Add(obj);


      }


}


static void Main (string[] args)


{


    List<int> lst1 = new List<int>();


  lst1.Add(2);


  lst1.Add(4);


    List<int> lst2 = new List<int>();


  Copy (lst1, lst2);


  Console.WriteLine(lst2.Count);


  }


}


   


Note:-The Copy() method is a generic method that works with the parameterized type T. When Copy() is invoked in Main (), the compiler figures out the specific type to use, based on the arguments presented to the Copy() method.

                    

Dotnet Related Tutorials

...more

New Dotnet Resources

...more

Copyright © 2010 VisualBuilder. All rights reserved