<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C# Help &#187; Generics</title>
	<atom:link href="http://www.csharphelp.com/tag/generics/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.csharphelp.com</link>
	<description>C# Tutorial and Guides</description>
	<lastBuildDate>Tue, 07 Feb 2012 01:03:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C# 4.0 &#8211; Covariance and Contravariance of Generics</title>
		<link>http://www.csharphelp.com/2010/02/c-4-0-covariance-and-contravariance-of-generics/</link>
		<comments>http://www.csharphelp.com/2010/02/c-4-0-covariance-and-contravariance-of-generics/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 03:27:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Contravariance]]></category>
		<category><![CDATA[Covariance]]></category>
		<category><![CDATA[Generics]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2149</guid>
		<description><![CDATA[Covariance allows casting of generic types to the base types, for example, IEnumerable&#60;X&#62; will be implicitly convertible an IEnumerable&#60;Y&#62; if X can implicitly be converted to Y. // List of strings IList&#60;string&#62; stringList = new List&#60;string&#62;(); // We can convert it to an Enumerable collection IEnumerable&#60;object&#62; Obj= strings; For this purpose IEnumerable is marked with [...]]]></description>
			<content:encoded><![CDATA[<p>Covariance allows casting of generic types to the base types, for example, IEnumerable&lt;X&gt; will be implicitly convertible an IEnumerable&lt;Y&gt; if X can implicitly be converted to Y.</p>
<pre>        // List of strings
        IList&lt;string&gt; stringList = new List&lt;string&gt;();

        // We can convert it to an Enumerable collection
        IEnumerable&lt;object&gt; Obj= strings;</pre>
<p>For this purpose IEnumerable is marked with the <strong>Out </strong>modifier.</p>
<pre>        public interface IEnumerable&lt;out T&gt; : IEnumerable {
            IEnumerator&lt;T&gt; GetEnumerator();
        }</pre>
<p>In C# 4.0, Contravariance allows for example, IComparer&lt;X&gt; to be cast to IComparer&lt;Y&gt; even if Y is a derived type of X. To achieve this IComparer should be marked with the <strong>In </strong> modifier.</p>
<pre>        public interface IComparer&lt;in T&gt; {
            public int Compare(T left, T right);
        }</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2010/02/c-4-0-covariance-and-contravariance-of-generics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generics in C#</title>
		<link>http://www.csharphelp.com/2007/08/generics-in-c-part-ii/</link>
		<comments>http://www.csharphelp.com/2007/08/generics-in-c-part-ii/#comments</comments>
		<pubDate>Fri, 17 Aug 2007 04:01:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Generics]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com.php5-3.dfw1-2.websitetestlink.com/?p=638</guid>
		<description><![CDATA[In my last article I focused on issues of typesafety and reusability when using ArrayLists.In this article we shall focus on how these issues of type safety andreusability are very nicely handled by Generics. All with the help ofcode again : using System; using System.Collections.Generic; using System.Text;   namespace GenericsSample { class Person { int [...]]]></description>
			<content:encoded><![CDATA[<p><span class="smallblack">In my last article I focused on issues of typesafety and reusability when using ArrayLists.In this article we shall focus on how these issues of type safety andreusability are very nicely handled by Generics. All with the help ofcode again :</span></p>
<pre>using System;
using System.Collections.Generic;
using System.Text;
 

namespace GenericsSample
{
class Person
{
int _Age;

public int Age
{
get { return _Age; }
set { _Age = value; }
}
String _Name;

public String Name
{
get { return _Name; }
set { _Name = value; }
}
String _Address;

public String Address
{
get { return _Address; }
set { _Address = value; }
}
String _Company;

public String Company
{
get { return _Company; }
set { _Company = value; }
}

public Person() { }
public Person(String Name)
{
this.Name = Name;
this.Age = 0;
this.Address = String.Empty;
this.Company = String.Empty;
}
public Person(String Name, int Age, String Address)
{
this.Name = Name;
this.Age = Age;
this.Address = Address;
}

}

class Program
{
static void Main(string[] args)
{
//Generic List Creation
//List is a Generic Class provided by .Net Framework 2.0
//System.Collections.Generics is the Namespace.

List&lt;person&gt; myPerson = new List&lt;person&gt;();
myPerson.Add(new Person("Saurabh"));
myPerson.Add(new Person("Manu"));
myPerson.Add(new Person("SomeOne", 24, "Gurgaon"));
myPerson.Add(new Person("SomeoneElse", 24, "Gurgaon"));

//myPerson.Add(new Car());// This is A Compile Time Error

foreach (Person p in myPerson)
{
Console.WriteLine(p.Name);
Console.WriteLine(p.Age);
Console.WriteLine(p.Address);
Console.WriteLine(p.Company);
}
Console.ReadLine();
}
}
}
</pre>
<p><span class="smallblack">Two classes can be seen in this code. ClassPerson is the class of which we want to create list for, and classProgram is the main class where we actually create the list of personsand operate upon them.</span></p>
<p><span class="smallblack">How Generics tackle the issues posed by ArrayLists?</span></p>
<p><span class="smallblack">In the above code example Generic List classhas been used to &#8220;contain&#8221; objects of type Person. At any time we canwe can have the Generic List contain any other type, as below:-</span></p>
<pre">//List of Ints
List&lt;int&gt; myInts = new List&lt;int&gt;();
myInts.Add(5);
myInts.Add(10);
myInts.Add(20);
foreach (int x in myInts)
{
Console.WriteLine(x);
}
Console.ReadLine();
&lt;/int&gt;&lt;/int&gt;</pre>
<p><span class="smallblack">The above code snippet indicates that the sameList class can be used to contain any datatype at any point of time,without requiring any kind of extra effort from the programmer&#8217;s side.</span></p>
<p><span class="smallblack">The syntax for using any kind of Generic Class is as under :- </span></p>
<p><span class="smallblack">GenericClass&lt;t&gt; objT = new GenericClass&lt;t&gt;(); &lt;/t&gt;&lt;/t&gt;</span></p>
<p><span class="smallblack">Where T is the datatype that&#8217; want to list,and GenericClass is the Generic Class which will wrap our desireddatatype (that&#8217;s the reason ,&#8221;contains&#8221; ,above has been marked in thedouble quotes and is marked bold <img src='http://www.csharphelp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . This Generic Class can be our owncustom Generic Class or the ones provided by the .Net Framework.</span></p>
<p><span class="smallblack">So technically, T gets replaced by thedatatype at compile type. And that&#8217;s the reaosn why a compile timeerror occurs when castinig is not done properly, it will be anInvalidCast Exception while using ArrayLists. Thus Generics enforcetype checking at complie time only, making life less difficult. </span></p>
<p><span class="smallblack">Performance is another area where Genericsmake it sweet when compared to ArrayLists. Since T is &#8220;replaced&#8221; by ourdatatype at comile time only so, no time and resources are wasted inboxing and unboxing the objects.</span></p>
<p><span class="smallblack">Thus Generics are a very powerful and nicefeature provided with .Net 2.0. Generics types are found sprinkledthroughout the .Net2.0 BCLs; however System.Collections.Genericsnamespace is chock full of them.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2007/08/generics-in-c-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generics in C#</title>
		<link>http://www.csharphelp.com/2007/08/generics-in-c-2-0-part-i/</link>
		<comments>http://www.csharphelp.com/2007/08/generics-in-c-2-0-part-i/#comments</comments>
		<pubDate>Thu, 16 Aug 2007 04:01:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Generics]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com.php5-3.dfw1-2.websitetestlink.com/?p=637</guid>
		<description><![CDATA[Generics are new feature provided with version2.0 of the Microsoft.Net framework. Generic classes and methods combinere-usability, type safety and efficiency in a way that therenon-generics counterparts do not/cannot. In this part some features of ArrayLists andthere shortcomings/limitations will be discussed. The contained in thisarticle has been written in Visual Studio 2005 in C#. Earlier ArrayLists [...]]]></description>
			<content:encoded><![CDATA[<p><span class="smallblack">Generics are new feature provided with version2.0 of the Microsoft.Net framework. Generic classes and methods combinere-usability, type safety and efficiency in a way that therenon-generics counterparts do not/cannot. </span></p>
<p><span class="smallblack">In this part some features of ArrayLists andthere shortcomings/limitations will be discussed. The contained in thisarticle has been written in Visual Studio 2005 in C#.</span></p>
<p><span class="smallblack">Earlier ArrayLists used to server the purpose,but that in a certain limit. Moreover, using ArrayLists to storedifferent types came as a good performance cost. At the client end itour desired type that we are storing in the ArrayList but internallythere&#8217;s much more that goes on.</span></p>
<p><span class="smallestblack"><span class="smallblack">Sample the code below:-</span></span></p>
<pre>
System.Collections.ArrayList myList = new System.Collections.ArrayList ();
myList.Add (22);
myList.Add ("C# Generics");
myList.Add (22.45);</pre>
<p><span class="smallblack">The usage convenience that is perceived abovecomes at a cost. Any reference or value type that is stored or added inthe ArrayList (myList) is implicitly upcasted to System.Object type.And while retrieval, the reverse happens &#8211; downcasting to theappropriate type takes place.</span></p>
<p><span class="smallblack">Moreover, there is compromise on the Type Safety front also. Consider the code below:-</span></p>
<pre>int item =0;

//This will cause an InvalidCastException
foreach (int x in myList)
{
Item = item+x;
}
</pre>
<p><span class="smallblack">The reason is evident.</span></p>
<p><span class="smallblack">In .Net 1.x type-safety was achieved bywriting your own typed ArrayList. But again, in that case re-usabilitywas a major issue. Consider the following code:-</span></p>
<pre>using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;</span></span>

namespace ArrayListSample
{
#region Person Class
class Person
{
String Name;
Int32 Age;
String Address;

#region Constructor
public Person() { }
public Person(String Name, Int32 Age, String Address)
{
Name = Name;
Age = Age;
Address = Address;
}

public Person(String Name, Int32 Age)
{
Name = Name;
Age = Age;
Address = String.Empty;
}
public Person(String Name, String Address)
{
Name = Name;
Age = 0;
Address = Address;
}
public Person(String Name)
{
Name = Name;
Age = 0;
Address = String.Empty;
}
#endregion
}
#endregion

#region PeopleCollection Class
class PeopleCollection : System.Collections.IEnumerable
{
private ArrayList arPeople = new ArrayList();

public PeopleCollection()
{

}

#region Methods
public void AddPeople(Person p)
{
arPeople.Add(p);
}

public void ClearPeople()
{
arPeople.Clear();
}

int Count;

public int PeopleCount
{
get { return Count; }
}
#endregion

#region IEnumerable Members

public System.Collections.IEnumerator GetEnumerator()
{
return arPeople.GetEnumerator();
}

#endregion
}
#endregion

class Client
{
public static void Main()
{
PeopleCollection myPeople = new PeopleCollection();
myPeople.AddPeople(new Person("Saurabh",24,"Gurgaon"));
myPeople.AddPeople(new Person("Manu"));

foreach (Person Person in myPeople)
{
Console.WriteLine(Person);
}
Console.ReadLine();
}
}
}
</pre>
<p><span class="smallblack">The above code does achieve type safety butthen we will have to write an almost identical custom collection foreach type we wish to contain. Because:-</span></p>
<p><span class="smallblack">myPeople.AddPeople(new Car()); </span></p>
<p><span class="smallblack">would be a compile time error, since thecode/approach above achieves type safety. So at the end of the day itwill be a big nightmare !!</span></p>
<p><span class="smallblack">So above we find some of the limitations ofArrayLists. In the next issue we will find how Genrics solve the issuesdiscussed above.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2007/08/generics-in-c-2-0-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Generics &#8211; An Introduction</title>
		<link>http://www.csharphelp.com/2007/08/c-generics-an-introduction/</link>
		<comments>http://www.csharphelp.com/2007/08/c-generics-an-introduction/#comments</comments>
		<pubDate>Sun, 12 Aug 2007 04:01:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Generics]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com.php5-3.dfw1-2.websitetestlink.com/?p=633</guid>
		<description><![CDATA[There is a reality; most of the developers confuse about C# Generics. Actually, there is no reason for that. If your background comes from C++ or Java you may understand more easily. Wha t tell you from your C++ knowledge o r from Java. Main concept is same here in C# with some better extensions [...]]]></description>
			<content:encoded><![CDATA[<p><span class="smallblack">There is a reality; most of the developers confuse about C# Generics. Actually, there is no reason for that. If your background comes from C++ or Java you may understand more easily. Wha t tell you from your C++ knowledge o r from Java. Main concept is same here in C# with some better extensions and easy usability. For example since C++ Templates uses compile time, C# Generics also a feature of the runtime. They are basically the capacity to have type parameters lying on your type. Generics refer to classes and methods that work homogeneously on values of different types. They are a type of data structure that contains code that remains the same; though, the data type of the parameters can change with each use. We can also call them parameterized types or parametric polymorphism.</span></p>
<p><span class="smallblack">The usage of generics within the data structure adjusts to the different data type of the passed variables. Briefly, a generic is a code template that can be applied to use the same code over and over again. Whenever the generic is used, it can be adapted for different data types without require to rewrite any of the internal code.</span></p>
<p><span class="smallblack">A List collection class can be a good example:</span></p>
<pre>List&lt;int&gt; Mylist1 = new List&lt;int&gt;();

// No boxing or casting necessary
list1.Add (5);

If we want to expand this code and make it more sensible, we can build
something like this:

using System;
using System.Collections;
using System.Text;

namespace TList
{
public class List&lt;template&gt; : CollectionBase
{
public List(){ }

public Template this[int index]
{
get { return (Template)List[index]; }
set { List[index] = value; }
}

public int Add(Template value)
{
return List.Add(value);
}
}
}
&lt;/template&gt;&lt;/int&gt;&lt;/int&gt;
</pre>
<p><span class="smallblack">If we decide to convert any fields to generic fields, we would just change their types to Template. <i>Continues&#8230;</i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2007/08/c-generics-an-introduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding C# Generics</title>
		<link>http://www.csharphelp.com/2006/11/understanding-c-generics/</link>
		<comments>http://www.csharphelp.com/2006/11/understanding-c-generics/#comments</comments>
		<pubDate>Wed, 08 Nov 2006 04:01:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Generics]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com.php5-3.dfw1-2.websitetestlink.com/?p=356</guid>
		<description><![CDATA[&#160; Introduction Before we starttampering around the code, lets understand why we need a generic type? Well inmost of application development we come across lots of situation where we need atype which can hold both value types and reference type. In this situation weneed a generic type which can do both. Before we start working [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:xx-small;"><b><br /></b></span><span class="smallblack"></span></p>
<p>&nbsp;</p>
<p style="text-align:justify;"><span class="smallblack"><b><span style="font-family:Verdana;font-size:small;">Introduction</span></b></span></p>
<p style="text-align:justify;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Before we starttampering around the code, lets understand why we need a generic type? Well inmost of application development we come across lots of situation where we need atype which can hold both value types and reference type. In this situation weneed a generic type which can do both. Before we start working on generic type,lets understand little about types in .Net as we know there are two fundamentalcategories of types</span></span></p>
<p><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">1]<b><span style="color:#800000;">valuetypes</span></b><span><span style="color:#800000;"><b>&nbsp;</b>&nbsp;</span>&nbsp;&nbsp;&nbsp;</span>(example : structures,primitive type,enums)</span></span></p>
<p><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">2]<span style="color:#800000;"><b>referencetypes</b> </span>(example : Classes,interface,arrays,strings etc..,)</span></span></p>
<p style="text-align:justify;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">So when you areusing any one of the above types they are bonded to that particular type. Forexample if we declare a structure it bonds to value type and when we declare aclass it bonds to reference type. Despite the fact that declaration syntax issame for both but semantics are distinct.<span>&nbsp; </span>Andin terms of memory allocation for value is on stack and where as for referencetype is on run-time heap.</span></span></p>
<p><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Assignment to a variable of value types createsa copy of the value.</span></span></p>
<p><span class="smallblack"><b><span style="font-family:Verdana;font-size:x-small;">For example:<span>&nbsp;&nbsp;</span></span></b></span></p>
<p><span class="smallblack"><b><span style="font-size:x-small;"><span><span style="font-family:Verdana;">//vtyp.cs</span></span><span style="font-family:Verdana;">&lt;o:p&gt;&lt;/o:p&gt;</span></span></b></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;"><span style="color:#0000ff;">using</span> System;<br /><span style="color:#0000ff;">public</span> <span style="color:#0000ff;"> class</span> valTyp<br />{<br /><span style="color:#0000ff;">public static void</span> Main()<br />{<br />Int32 I=10,J=0;<br />J=I;&nbsp;<br />// this will create a copy value I into J&nbsp;<br />I++;<br />Console.WriteLine( &quot;I value: {0}, J value {1}&quot;,I,J);<br />}<br />}<br /></span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><b><span style="font-family:Verdana;font-size:x-small;">Output:</span></b></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">D:\vstudio&gt;vtype<br />I value: 11, J value 10</span></span></p>
<p style="text-align:justify;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">The assignment tothe local variable <b>J</b> does not impact the local variable <b>I</b> becauseboth local variables are of a value type (the type <b>Integer</b>) and eachlocal variable of a value type has its own storage.</span></span></p>
<p style="text-align:justify;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">But Assignment to avariable of a reference type creates a copy of the reference, not a copy of thevalue being referenced let?s see this with an example below.</span></span></p>
<p><span class="smallblack"><b><span style="font-family:Verdana;font-size:x-small;">For example:<span>&nbsp;&nbsp;</span></span></b></span></p>
<p><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">//<b>vref.cs<br /></b><b>&lt;o:p&gt;&lt;/o:p&gt;</b><span style="color:#0000ff;">using</span> System;<br /><span style="color:#0000ff;">public class</span> Cclass<br />{<br /><span style="color:#0000ff;"> public int</span> val = 0;<br />}<br /><span style="color:#0000ff;">public class </span> refTyp<br />{<br /><span style="color:#0000ff;">public static void</span> Main()<br />{<br />Cclass ref1 = new Cclass();<br />Cclass ref2=ref1;<br />ref2.val = 555;<br />Console.WriteLine( &quot;Before increament Ref1 value: {0}, Ref2 value {1}&quot;,ref1.val,ref2.val);<br />ref2.val++;<br />Console.WriteLine( &quot;After increament Ref1 value: {0}, Ref2 value {1}&quot;,ref1.val,ref2.val);<br />&nbsp;}<br />}<br /><b>Output:</b></span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">D:\vstudio&gt;vref<br />Before increament Ref1 value: 555, Ref2 value 555<br />After increament Ref1 value: 556, Ref2 value 556</span></span></p>
<p style="text-align:justify;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">In contrast withvalue type the assignment to reference type variable <code>ref2.val = 555,</code>affects the object that both <code>ref1</code> and <code>ref2</code> reference.</span></span></p>
<p style="text-align:justify;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">From above we seethat value types and reference types are different. So to create a <b><span style="color:#800000;">Generictype</span></b> we need something which neither of them. In .net we have theroot type <b><span style="color:#800000;">Object</span></b> (alias <b><span style="color:#800000;">System.Object</span></b>)is special in that it is neither a reference type nor a value type, and may notbe instantiated. So a variable of type <b><span style="color:#800000;">Object</span></b>can either contain a value type or a reference type. Yes there is question,which arises in mind why <b><span style="color:#800000;">Object</span></b> class canhandle both?</span></span></p>
<p><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">The answer is bothreference and value types are derived from the base class <b><span style="color:#800000;">Object</span>.</b>So using the concept <b>Boxing</b> and <b>unboxing</b> we can treat any typeas an object. So what does boxing do, So where its necessary for a value type tobehave like a object boxing is done which wrappers the value type to look like areference object (allocated on heap), and the value type?s value is copiedinto it. Then this complete wrapper is marked so that <b>CLR</b> knows that itcontains a value type. Similarly the reverse process is called as <b>unboxing</b>.</span></span></p>
<p><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Let?s see a sample code using <b><span style="color:#800000;">Object</span></b>class as Generic type:</span></span></p>
<p><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">// <b><span style="text-decoration:underline;">objapp.cs&lt;o:p&gt;&lt;/o:p&gt;</span></b></span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;"><span style="color:#0000ff;">using</span>System;</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;"><span style="color:#0000ff;">publicclass </span>Myclass</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">{</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;</span></span></span><span class="sma</p>
<p>llblack"<br />
><span style="font-family:Verdana;font-size:x-small;"><span style="color:#0000ff;">public string</span>name=&quot;Hello from Myclass&quot;;</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;"><span style="color:#0000ff;">public void</span>doSomething()</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">{</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Console.WriteLine(name);</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">}</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">}</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;"><span style="color:#0000ff;">publicclass</span> ObjApp{</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;"><span style="color:#0000ff;">publicstatic void</span> CheckType(object o)</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">{</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;"></span></span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;"><span style="color:#0000ff;">if</span>(o <span style="color:#0000ff;">is</span> string)</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">{</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Console.WriteLine (&quot;o isString&quot;);</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">string s = (string)o;</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Console.WriteLine (s);</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">}</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-size:x-small;"><span><span style="font-family:Verdana;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-family:Verdana;"><span style="color:#0000ff;">elseif </span>(o <span style="color:#0000ff;">is</span> Int32[])</span></span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">{</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Console.WriteLine (&quot;o is Int32array&quot;);</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Int32[] ins=(Int32[])o;</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">for(Int32 i=0;i&lt; ins.Length;i++)</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Console.WriteLine (ins[i]);</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">}</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;"><span style="color:#0000ff;">elseif </span>(o <span style="color:#0000ff;">is</span> Myclass)</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">{</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Console.WriteLine (&quot;o isMyclass&quot;);</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Myclass a = (Myclass)o;</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">a.doSomething();</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">}</span></span></p>
<p style="margin:0in 0in 0.0</p>
<p>001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">}</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;"><span style="color:#0000ff;">public staticvoid</span> Main(){</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">//Create an array of Object references</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Object[]objs = <span style="color:#0000ff;">new</span> Object[3];<span>&nbsp;&nbsp;&nbsp;</span></span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-size:x-small;"><span><span style="font-family:Verdana;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-family:Verdana;">objs[0]= &quot;Some String&quot;;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>// create a new string</span></span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">objs[1]= <span style="color:#0000ff;">new</span> Int32[]{1, 2, 4}; // create a new Int32 array</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">objs[2]= <span style="color:#0000ff;">new</span> Myclass();<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>// create a newObject instance</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;"><span style="color:#0000ff;">for</span>(Int32ind = 0; ind &lt; 3; ind++)</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">{</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Console.WriteLine(&quot;Array index{0} refers to {1}&quot;,ind, objs[ind]);</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">ObjApp.CheckType(objs[ind]);<span>&nbsp;&nbsp;&nbsp;&nbsp; </span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-size:x-small;"><span><span style="font-family:Verdana;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-family:Verdana;">}</span></span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&lt;o:p&gt;&lt;/o:p&gt;</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-size:x-small;"><span><span style="font-family:Verdana;">&nbsp;&nbsp;</span></span><span style="font-family:Verdana;">}</span></span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">}</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&lt;o:p&gt;&lt;/o:p&gt;</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><b><span style="font-family:Verdana;font-size:x-small;">Output:</span></b></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Arrayindex 0 refers to Some String</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">o isString</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">SomeString</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Arrayindex 1 refers to System.Int32[]</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">o isInt32 array</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">1</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">2</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">4</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Arrayindex 2 refers to Myclass</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">o isMyclass</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Hellofrom Myclass</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&lt;o:p&gt;&lt;/o:p&gt;</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;&lt;o:p&gt;&lt;/o:p&gt;</span></span></p>
<p style="margin:0in 0in 0.0001pt;text-align:justify;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">Inthe above program if you see <b><span style="color:#800000;">Object</span></b> classcan take any type and in <b><span style="color:#800000;">CheckType</span></b> memberfunction we are sending the generic type (<b><span style="color:#800000;">Object</span></b>)and using <b><span style="color:#0000ff;">is</span></b> operator we are checking thetype which is passed as parameter to the function , Then using <b><span style="color:#800000;">Boxing</span></b>we can point to the specific type and use for further processing.</span></span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack"><span style="font-family:Verdana;font-size:x-small;">&nbsp;</span>&lt;o:p&gt;&lt;/o:p&gt;</span></p>
<p style="margin:0in 0in 0.0001pt;"><span class="smallblack">&nbsp;&lt;o:p&gt;&lt;/o:p&gt;</span></p>
<p class="heading2"><span style="font-family:Verdana;font-size:10pt;"><span class="smallblack">Furtherreading &lt;o:p&gt;&lt;/o:p&gt;</span></span></p>
<ol><span class="smallblack">
<li class="MsoNormal"><span style="font-family:Verdana;font-size:10pt;"><a href="http://msdn.microsoft.com/net"><span>.Net</span></a><span>&nbsp; </span>More information on .Net technologies</span></li>
<p></span></ol>
<p class="MsoNormal"><span class="smallblack">&nbsp;<b><span style="text-decoration:underline;"><span style="co</p>
<p>lor:#006699;font-family:Verdana;font-size:10pt;">Aboutthe Author:</span></span></b><span style="font-family:Verdana;font-size:10pt;"> <b>ChandraHundigam</b> has Master degree is Computer Application, Working on MicrosoftTechnologies for past 5 years. Presently working as consultant for a US basedcompany. His area of interests is Emerging Technologies.</span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2006/11/understanding-c-generics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

