<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6110498128576801903</id><updated>2024-08-30T23:49:47.121-07:00</updated><title type='text'>Generic Methods</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://ravishankarjsr.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6110498128576801903/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://ravishankarjsr.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ravi Vanapalli</name><uri>http://www.blogger.com/profile/10796664841092275131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOnjlRoSeGpZi58EYoE9hh_agAdhLQEEcMediKrVW9RYwHsofdL3nSXF1qgjX4FwYK6Il63ARx4rY2Iwj-qvhy6dIEz8SmsD0ZxYFwKzP24onGHduvGTJUH4j_yPjx0yM/s220/Photo.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6110498128576801903.post-8298276558057521199</id><published>2010-08-31T09:34:00.000-07:00</published><updated>2010-09-26T08:48:35.821-07:00</updated><title type='text'>Generic Method</title><content type='html'>&lt;p style=&quot;MARGIN-BOTTOM: 0in;font-family:times new roman;&quot; &gt;&lt;span style=&quot;font-size:85%;&quot;&gt;Generic in C# means common to or applicable to an entire class. As most of the developers think Generic is to define type-safe data structures without committing to actual data types, but is it the only reason why Generics are for?&lt;span style=&quot;font-size:0;&quot;&gt;The answer is BIG NO&lt;/span&gt;.&lt;/span&gt;&lt;/p&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family:times new roman;font-size:85%;&quot;&gt;In this article we will focus on what other important aspect can be achieved in our daily programming by using Generic. We will see how to use Generic and avoid method overloading. &lt;/span&gt;&lt;p style=&quot;MARGIN-BOTTOM: 0in;font-family:times new roman;&quot; &gt;&lt;span style=&quot;font-size:85%;&quot;&gt;The below program shows the use of method overloading to display content of int, double &amp;amp; char array.&lt;/span&gt;&lt;/p&gt;&lt;span style=&quot;font-family:times new roman;font-size:85%;&quot;&gt;When we run the program output would be following:&lt;/span&gt;&lt;br /&gt;&lt;pre style=&quot;COLOR: rgb(0,0,153)&quot;&gt;using System;&lt;br /&gt;class OverloadedMethods&lt;br /&gt;{&lt;br /&gt;static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt;  // create arrays of int, double and char&lt;br /&gt;  int[] intArray = { 1, 2, 3, 4, 5, 6 };&lt;br /&gt;  double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };&lt;br /&gt;  char[] charArray = { &#39;H&#39;, &#39;E&#39;, &#39;L&#39;, &#39;L&#39;, &#39;O&#39; };&lt;br /&gt;&lt;br /&gt;  Console.WriteLine(&quot;Array intArray contains:&quot;);&lt;br /&gt;  DisplayArray(intArray); // pass an int array argument&lt;br /&gt;  Console.WriteLine(&quot;Array doubleArray contains:&quot;);&lt;br /&gt;  DisplayArray(doubleArray); // pass a double array argument&lt;br /&gt;  Console.WriteLine(&quot;Array charArray contains:&quot;);&lt;br /&gt;  DisplayArray(charArray); // pass a char array argument&lt;br /&gt;} // end Main&lt;br /&gt;// output int array&lt;br /&gt;static void DisplayArray(int[] inputArray)&lt;br /&gt;{&lt;br /&gt;  foreach (int element in inputArray)&lt;br /&gt;      Console.Write(element + &quot; &quot;);&lt;br /&gt;  Console.WriteLine(&quot;\n&quot;);&lt;br /&gt;} // end method DisplayArray&lt;br /&gt;// output double array&lt;br /&gt;static void DisplayArray(double[] inputArray)&lt;br /&gt;{&lt;br /&gt;  foreach (double element in inputArray)&lt;br /&gt;      Console.Write(element + &quot; &quot;);&lt;br /&gt;  Console.WriteLine(&quot;\n&quot;);&lt;br /&gt;} // end method DisplayArray&lt;br /&gt;// output char array&lt;br /&gt;static void DisplayArray(char[] inputArray)&lt;br /&gt;{&lt;br /&gt;  foreach (char element in inputArray)&lt;br /&gt;      Console.Write(element + &quot; &quot;);&lt;br /&gt;  Console.WriteLine(&quot;\n&quot;);&lt;br /&gt;} // end method DisplayArray&lt;br /&gt;} // end class OverloadedMethods&lt;br /&gt;&lt;/pre&gt;&lt;p style=&quot;MARGIN-BOTTOM: 0in&quot;&gt;&lt;a name=&quot;ctl00_ContentPlaceHolder1_lblDescription&quot;&gt;&lt;/a&gt;&lt;span style=&quot;COLOR: rgb(0,0,139)&quot;&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;For Array intArray contains:&lt;br /&gt;1 2 3 4 5 6&lt;br /&gt;&lt;br /&gt;For Array doubleArray contains:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;COLOR: rgb(0,69,134)&quot;&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;span lang=&quot;zxx&quot;&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;21.1, 22.2, 23.3, 24.4, 25.5&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;MARGIN-BOTTOM: 0in&quot;&gt;&lt;span style=&quot;COLOR: rgb(0,69,134)&quot;&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;span lang=&quot;zxx&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;COLOR: rgb(0,0,139)&quot;&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;For Array charArray contains:&lt;br /&gt;H E L L O&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;MARGIN-BOTTOM: 0in&quot; face=&quot;courier new&quot;&gt;&lt;style type=&quot;text/css&quot;&gt;  &lt;!--   @page { margin: 0.79in }   P { margin-bottom: 0.08in }  --&gt;  &lt;/style&gt;&lt;/p&gt;&lt;p style=&quot;MARGIN-BOTTOM: 0in;font-family:times new roman;&quot; &gt;&lt;span style=&quot;font-size:85%;&quot;&gt;This looks pretty simple, but did you notice we had to write the same logic thrice for int, double and char. What if we had do write similar processing for all the data types, it would have taken hundred&#39;s of line.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;FONT-FAMILY: courier new; MARGIN-BOTTOM: 0in&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;a name=&quot;ctl00_ContentPlaceHolder1_lblDescription1&quot;&gt;&lt;/a&gt;&lt;a name=&quot;ctl00_ContentPlaceHolder1_lblDescription2&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;span style=&quot;COLOR: rgb(0,0,0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:times new roman;&quot;&gt;To overcome this from C# 2.0 Generic methods have been introduced. If we have similar operation to be performed by several overloaded methods we can write a single generic method declaration that can be called at different times with arguments of different types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately. All generic method declarations have a type parameter list delimited by angle brackets (&lt;&gt; in this example) that follows the method&#39;s name. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;MARGIN-BOTTOM: 0in;font-family:times new roman;&quot; &gt;&lt;span style=&quot;COLOR: rgb(0,0,0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:times new roman;&quot;&gt;Below we see the same code written using Generic method.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;using System;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;using System.Collections.Generic;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;class MethodGeneric&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;    static void Main(string[] args)&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;        // create arrays of int, double and char&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;        int[] intArray = { 1, 2, 3, 4, 5, 6 };&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;        double[] doubleArray = { 21.1, 22.2, 23.3,24.4, 25.5};&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;        char[] charArray = { &#39;H&#39;, &#39;E&#39;, &#39;L&#39;, &#39;L&#39;, &#39;O&#39;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;        Console.WriteLine(&quot;Array intArray contains:&quot;);&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;        PrintArray(intArray); // pass an int array argument&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;        Console.WriteLine(&quot;Array doubleArray contains:&quot;);&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;        PrintArray(doubleArray); // pass a double array argument&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;        Console.WriteLine(&quot;Array charArray contains:&quot;);&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;        PrintArray(charArray); // pass a char array argument&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;    } // end Main&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;    // output array of all types&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;    static void PrintArray&lt;/span&gt;&lt;e&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;(E[] inputArray)&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;        foreach (E element in inputArray)&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;            Console.Write(element + &quot; &quot;);&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;        Console.WriteLine(&quot;\n&quot;);&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;    } // end method PrintArray&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(0,0,153)&quot;&gt;} // end class MethodGeneric&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/e&gt;&lt;/pre&gt;&lt;span style=&quot;COLOR: rgb(0,0,0);font-family:times new roman;font-size:85%;&quot;  &gt;This program has the same output as the previous one, but we reduced the number of lines in the program. A generic method’s body is declared like that of any other method. Just like type declarations, method declarations can be generic i.e., parameterized by one or more type parameters. A type parameter is an identifier that is used in place of actual type names. The type parameters can be used to declare the return type, the parameter types and the local variable types in a generic method declaration. The type parameters act as placeholders for the types of the arguments passed to the generic method.&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://ravishankarjsr.blogspot.com/feeds/8298276558057521199/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ravishankarjsr.blogspot.com/2010/08/generic-method.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6110498128576801903/posts/default/8298276558057521199'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6110498128576801903/posts/default/8298276558057521199'/><link rel='alternate' type='text/html' href='http://ravishankarjsr.blogspot.com/2010/08/generic-method.html' title='Generic Method'/><author><name>Ravi Vanapalli</name><uri>http://www.blogger.com/profile/10796664841092275131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOnjlRoSeGpZi58EYoE9hh_agAdhLQEEcMediKrVW9RYwHsofdL3nSXF1qgjX4FwYK6Il63ARx4rY2Iwj-qvhy6dIEz8SmsD0ZxYFwKzP24onGHduvGTJUH4j_yPjx0yM/s220/Photo.jpg'/></author><thr:total>2</thr:total></entry></feed>