<?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; Collections</title>
	<atom:link href="http://www.csharphelp.com/tag/collections/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# Collections &#8211; What is a Collection?</title>
		<link>http://www.csharphelp.com/2007/09/c-collections-what-is-a-collection/</link>
		<comments>http://www.csharphelp.com/2007/09/c-collections-what-is-a-collection/#comments</comments>
		<pubDate>Wed, 05 Sep 2007 04:01:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Collections]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com.php5-3.dfw1-2.websitetestlink.com/?p=657</guid>
		<description><![CDATA[Admitted, we blew it in the first version of the framework with System.Collections.ICollection, which is next to useless. But we fixed it up pretty well when generics came along in .NET framework 2.0: System.Collections.Generic.ICollection&#60;T&#62; lets you Add and Remove elements, enumerate them, Count them and check for membership. Obviously from then on, everyone would implement [...]]]></description>
			<content:encoded><![CDATA[<p><span class="smallblack"><span style="font-family: Verdana; color: #000080;">Admitted, we blew it in the first version of the framework with </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">System.Collections.ICollection</span></span><span style="font-family: Verdana; color: #000080;">, which is next to useless. But we fixed it up pretty well when generics came along in .NET framework 2.0: </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">System.Collections.Generic.ICollection&lt;T&gt;</span></span><span style="font-family: Verdana; color: #000080;"> lets you </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">Add</span></span><span style="font-family: Verdana; color: #000080;"> and </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">Remove</span></span><span style="font-family: Verdana; color: #000080;"> elements, enumerate them, </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">Count</span></span><span style="font-family: Verdana; color: #000080;"> them and check for membership.</span></span></p>
<p><span class="smallblack"><span style="font-family: Verdana; color: #000080;">Obviously from then on, everyone would implement </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">ICollection&lt;T&gt;</span></span><span style="font-family: Verdana; color: #000080;"> every time they make a collection, right? Not so. Here is how we used LINQ to learn about what collections <em>really</em> are, and how that made us change our language design in C# 3.0.</span></span></p>
<p class="Heading3web" style="margin: auto 0in;"><span class="smallblack"><strong><span style="font-family: Verdana; color: #000080;">Collection initializers</span></strong></span></p>
<p><span class="smallblack"><span style="font-family: Verdana; color: #000080;">With LINQ, the <strong><span style="text-decoration: underline;">L</span></strong>anguage <strong><span style="text-decoration: underline;">IN</span></strong>tegrated <strong><span style="text-decoration: underline;">Q</span></strong>ueryframework that we&#8217;re shipping in Orcas, we&#8217;re enabling a moreexpression-oriented style of programming. For instance it should bepossible to create and intialize an object within one expression. Forcollections, initialization typically amounts to adding an initial setof elements. Hence collection initializers in C# 3.0 look like this:</span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><span style="color: #008000;"><span style="font-family: Lucida Console;"><span> </span>new MyNames { &#8220;&lt;st1:personname w:st=&#8221;on&#8221;&gt;Luke Hoban&lt;/st1:personname&gt;&#8221;, &#8220;&lt;st1:personname w:st=&#8221;on&#8221;&gt;Karen Liu&lt;/st1:personname&gt;&#8221;, &#8220;&lt;st1:personname w:st=&#8221;on&#8221;&gt;Charlie Calvert&lt;/st1:personname&gt;&#8221; }</span></span></span></p>
<p><span class="smallblack"><span style="font-family: Verdana; color: #000080;">The meaning of this new syntax is simply to create an instance of </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">MyNames</span></span><span style="font-family: Verdana; color: #000080;">using its no-arg constructor (constructor arguments can be supplied ifnecessary) and call its Add method with each of the strings.</span></span></p>
<p><span class="smallblack"><span style="font-family: Verdana; color: #000080;">So what types do we allow collection initializers <em>on</em>? Easy: collection types. What are those? Obvious: types that implement </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">ICollection&lt;T&gt;</span></span><span style="font-family: Verdana; color: #000080;">. This is a nice and easy design &#8211; </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">ICollection&lt;T&gt;</span></span><span style="font-family: Verdana; color: #000080;"> ensures that you have an </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">Add</span></span><span style="font-family: Verdana; color: #000080;">method so obviously that is the one that gets called for each elementin the collection initializer. It is strongly typed, too &#8211; theinitializer can contain only elements of the appropriate element type.In the above new expression, </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">MyNames</span></span><span style="font-family: Verdana; color: #000080;"> would be a class that implements </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">ICollection&lt;string&gt;</span></span><span style="font-family: Verdana; color: #000080;"> and everything works smoothly from there.</span></span></p>
<p><span class="smallblack"><span style="color: #000080;"><span style="font-family: Verdana;">There&#8217;s just one problem: <em>Nobody implements </em></span></span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">ICollection&lt;T&gt;</span></span><span style="font-family: Verdana; color: #000080;">!</span></span></p>
<p class="Heading3web" style="margin: auto 0in;"><span class="smallblack"><strong><span style="font-family: Verdana; color: #000080;">LINQ to LINQ</span></strong></span></p>
<p><span class="smallblack"><span style="font-family: Verdana; color: #000080;">Well, <em>nobody</em>is a strong word. But we did an extensive study of our own frameworkclasses, and found only a few that did. How? Using LINQ of course. Thefollowing query does the trick:</span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><span style="color: #008000;"><span style="font-family: Lucida Console;"><span> </span>from name in assemblyNames</span></span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><span style="color: #008000;"><span style="font-family: Lucida Console;"><span> </span>select Assembly.LoadWithPartialName(name) into a</span></span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><span style="color: #008000;"><span style="font-family: Lucida Console;"><span> </span>from c in a.GetTypes()</span></span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><span style="color: #008000;"><span style="font-family: Lucida Console;"><span> </span>where c.IsPublic &amp;&amp;</span></span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><span style="color: #008000;"><span style="font-family: Lucida Console;"><span> </span>c.GetConstructors().Any(m =&gt; m.IsPublic) &amp;&amp;</span></span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><span style="color: #008000;"><span style="font-family: Lucida Console;"><span> </span>GetInterfaceTypes(c).Contains(typeof(ICollection&lt;&gt;)) </span></span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><span style="color: #008000;"><span style="font-family: Lucida Console;"><span> </span>select c.FullName;<span style="color: navy; font-family: Verdana;">&lt;o:p&gt;&lt;/o:p&gt;</span></span></span></span></p>
<p><span class="smallblack"><span style="font-family: Verdana; color: #000080;">Let.s go through this query a little bit and see what it does. For each </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">name</span></span><span style="font-family: Verdana; color: #000080;"> in a list of </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">assemblyNames</span></span><span style="font-family: Verdana; color: #000080;"> that we pre-baked for the purpose, load up the corresponding assembly:</span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><span style="color: #008000;"><span style="font-family: Lucida Console;"><span> </span>from name in assemblyNames</span></span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><br />
<span><span style="font-family: Lucida Console;"><span> </span>select Assembly.LoadWithPartialName(name)</span></span></span></p>
<p><span class="smallblack"><span style="font-family: Verdana; color: #000080;">One at a time, put the reflection objects representing these assemblies </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">into a</span></span><span style="font-family: Verdana; color: #000080;">, and for each assembly </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">a</span></span><span style="font-family: Verdana; color: #000080;"> run through the types </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">c</span></span><span style="font-family: Verdana; color: #000080;"> defined in there:</span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><span style="color: #008000;"><span style="font-family: Lucida Console;"><span> </span>from c in a.GetTypes()</span></span></span></p>
<p><span class="smallblack"><span style="font-family: Verdana; color: #000080;">Filter through, keeping each type only if it </span></span></p>
<p style="margin-left: 0.5in; text-indent: -0.25in;"><span class="smallblack"><span style="color: #008000;"><span class="Codeinline"><span><span><span style="font-family: Lucida Console;">a)</span><span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span></span><span class="Codeinline"><span style="font-family: Lucida Console;">IsPublic&lt;o:p&gt;&lt;/o:p&gt;</span></span></span></span></p>
<p style="margin-left: 0.5in; text-indent: -0.25in;"><span class="smallblack"><span style="color: #000080;"><span><span><span style="font-family: Verdana;">b)</span><span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><span style="font-family: Verdana;">has </span></span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">Any</span></span><span style="font-family: Verdana; color: #000080;"> constructor that </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">IsPublic</span></span></span></p>
<p style="margin-left: 0.5in; text-indent: -0.25in;"><span class="smallblack"><span style="color: #000080;"><span><span><span style="font-family: Verdana;">c)</span><span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><span style="font-family: Verdana;">implements </span></span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">ICollection&lt;T&gt;</span></span><span style="font-family: Verdana; color: #000080;"> for some </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">T</span></span><span style="font-family: Verdana; color: #000080;">:</span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><span style="color: #008000;"><span style="font-family: Lucida Console;"><span> </span>where c.IsPublic &amp;&amp;</span></span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><span style="color: #008000;"><span style="font-family: Lucida Console;"><span> </span>c.GetConstructors().Any(m =&gt; m.IsPublic) &amp;&amp;</span></span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><span style="color: #008000;"><span style="font-family: Lucida Console;"><span> </span>GetInterfaceTypes(c).Contains(typeof(ICollection&lt;&gt;)) </span></span></span></p>
<p><span class="smallblack"><span style="font-family: Verdana; color: #000080;">For those that pass this test, select out their full name:</span></span></p>
<p class="Code" style="margin: 0in 0in 0pt;"><span class="smallblack"><span style="color: #008000;"><span style="font-family: Lucida Console;"><span> </span>select c.FullName;<span style="color: navy; font-family: Verdana;">&lt;o:p&gt;&lt;/o:p&gt;</span></span></span></span></p>
<p><span class="smallblack"><span style="font-family: Verdana; color: #000080;">Nothing to it, really.</span></span></p>
<p class="Heading3web" style="margin: auto 0in;"><span class="smallblack"><strong><span style="font-family: Verdana; color: #000080;">What is a collection?</span></strong></span></p>
<p><span class="smallblack"><span style="font-family: Verdana; color: #000080;">What did we find then? Only 14 of our own (public) classes (with public constructors) implement </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">ICollection&lt;T&gt;</span></span><span style="font-family: Verdana; color: #000080;">!Obviously there are a lot more collections in the framework, so it wasclear that we needed some other way of telling whether something is acollection class. LINQ to the rescue once more: With modified versionsof the query it was easy to establish that among our public classeswith public constructors there are:</span></span></p>
<p style="margin-left: 0.2in; text-indent: -0.2in;"><span class="smallblack"><span style="color: #000080;"><span style="font-family: Symbol;"><span>&lt;&lt;&lt;&lt;&lt;&lt;<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><span style="font-family: Verdana;">189 that have a public </span></span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">Add</span></span><span style="font-family: Verdana; color: #000080;"> method and implement </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">System.Collections.IEnumerable</span></span></span></p>
<p style="margin-left: 0.2in; text-indent: -0.2in;"><span class="smallblack"><span style="color: #000080;"><span style="font-family: Symbol;"><span>&lt;&lt;&lt;&lt;&lt;&lt;<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><span style="font-family: Verdana;">42 that have a public </span></span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">Add</span></span><span style="font-family: Verdana; color: #000080;"> method but do not implement </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">System.Collections.IEnumerable</span></span></span></p>
<p><span class="smallblack"><span style="font-family: Verdana; color: #000080;">If youlook at the classes returned by these two queries, you realize thatthere are essentially two fundamentally different meanings of the name .</span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">Add</span></span><span style="font-family: Verdana; color: #000080;">.:</span></span></p>
<p style="margin-left: 0.5in; text-indent: -0.25in;"><span class="smallblack"><span style="color: #000080;"><span><span><span style="font-family: Verdana;">a)</span><span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><span style="font-family: Verdana;">Insert the argument into a collection, or </span></span></span></p>
<p style="margin-left: 0.5in; text-indent: -0.25in;"><span class="smallblack"><span style="color: #000080;"><span><span><span style="font-family: Verdana;">b)</span><span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-s ize-adjust: none; font-stretch: normal;"> </span></span></span><span style="font-family: Verdana;">Return the arithmetic sum of the argument and the receiver. </span></span></span></p>
<p><span class="smallblack"><span style="font-family: Verdana; color: #000080;">People are actually very good at (directly or indirectly) implementing the nongeneric </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">IEnumerable</span></span><span style="font-family: Verdana; color: #000080;"> interface when writing collection classes, so that turns out to be a pretty reliable indicator of whether an </span><span class="Codeinline"><span style="font-family: Lucida Console; color: #008000;">Add</span></span><span style="font-family: Verdana; color: #000080;"> method is the first or the second kind. Thus for our purposes the operational answer to the headline question becomes:</span></span><i>Continues&#8230;</i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2007/09/c-collections-what-is-a-collection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing Collection Classes In .NET</title>
		<link>http://www.csharphelp.com/2006/07/developing-collection-classes-in-net/</link>
		<comments>http://www.csharphelp.com/2006/07/developing-collection-classes-in-net/#comments</comments>
		<pubDate>Wed, 19 Jul 2006 04:01:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Collections]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com.php5-3.dfw1-2.websitetestlink.com/?p=244</guid>
		<description><![CDATA[Collection resembles to grouping together theset of objects with the same characteristics, in programming paradigmit resembles to the set of object instances of the same class.In .NET, class libraries provides the collection interfaces that areused to develop one&#39;s own collection classes. The basic functionalityfor the collections is made available by inheriting the abstract(MustInherit in VB) [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:xx-small;"><b><br /></b></span><span class="smallblack"><a href="mailto:yogeshpatange@yahoo.com"></a></span></p>
<p><span class="smallblack">Collection resembles to grouping together theset of objects with the same characteristics, in programming paradigmit resembles to the set of object instances of the same class.In .NET, class libraries provides the collection interfaces that areused to develop one&#39;s own collection classes. The basic functionalityfor the collections is made available by inheriting the abstract(MustInherit in VB) Class CollectionBase from the System.Collectionnamespace. The CollectionBase class provides the implementations forthe Clear method and the Count property, and it maintains a Protectedproperty called List, which it uses for the internal storage andorganization of the object instances. Other methods such as Add andRemove as well as the Item property requires implementation to beprovided.</span></p>
<p><span class="smallblack">Steps:<br />Create the class for which the collection class is to be implemented:<br />For our discussion we will consider the following class definition.</span></p>
<p><span class="smallblack">class Leaf {<br /> private int intAge;<br /> private String strName;</p>
<p> // Default Constructor <br /> public Leaf() {}</p>
<p> // gets and sets the age value<br /> public int Age {<br /> get {<br /> return intAge;<br /> }<br /> set {<br /> intAge = value;<br /> }<br /> }</p>
<p> // gets and sets the strName variable<br /> public string Name {<br /> get {<br /> return strName;<br /> }<br /> set {<br /> strName = value;<br /> }<br /> }<br />}<br /></span>
<p><span class="smallblack">The class leaf has private variables intAgeand strName and exposes them through public properties Age and Name tothe outside world.<br /> Now let us write the collection class. We will name this class as Leafs.</span></p>
<p><span class="smallblack"> class Leafs: CollectionBase { }</span></p>
<p><span class="smallblack">As the above definition suggests the Leafsclass inherits from the CollectionBase abstract class. Inheriting fromthe CollectionBase class implicitly provides the List collection whichis of type IList and which represents a collection of objects that canbe individually indexed. The next step is to provide the implementationfor the methods in CollectionBase.</span></p>
<p><span class="smallblack">Implement the Add, Remove methods and Item property.</span></p>
<p><span class="smallblack">// Method implementation from the CollectionBase class<br />public void Add(Leaf aLeaf){ <br /> List.Add(aLeaf);<br />} </p>
<p>// Method implementation from the CollectionBase class<br />public void Remove(int index){<br /> // Check to see if there is a Leaf at the supplied index.<br /> if (index &gt; Count &#8211; 1 || index &lt; 0) {<br /> // Handle the error that occurs if the valid page index is <br /> // not supplied. <br /> // This exception will be written to the calling function <br /> throw new Exception(&quot;Index out of bounds&quot;); <br /> } <br /> List.RemoveAt(index); <br />} <br />// Method implementation from the CollectionBase class<br />public Leaf Item(int Index) { <br /> // The appropriate item is retrieved from the List object and <br /> // explicitly cast to the Leaf type, then returned to the <br /> // caller. <br /> return (Leaf) List[Index]; <br />}<br /></span>
<p><span class="smallblack">The above procedure will make the fullfunctional collection class for containing the collection instances ofyour own class definition. We can still improve the handling of theabove class, by adding the functionality to support the class to beused with foreach.. in.. construct. For collection class to support theenumeration functionality it can be extended to implement theIEnumerable interface.</span></p>
<p><span class="smallblack">Steps: </span></p>
<p><span class="smallblack">Extend your Collection class definition to implement IEnumerable interface:</span></p>
<p><span class="smallblack">class Leafs: CollectionBase, IEnumerable{}</span></p>
<p><span class="smallblack">Every collection is required to return theenumerator interface for the classes that provide the collectionenumeration functionality. The GetEnumerator() function formIEnumerable interface returns the Enumerator object for the collection.So we have to define the enumerator object for our collection and thenrewrite the IEnumerable.GetEnumerator() function to return theIEnumerator interface.Following is the code snippet for class Enumerator for our Leafscollection and we will name it as LeafEnumerator.</span></p>
<p><span class="smallblack">/* Define the Enumeration class to return */ <br />public class LeafEnumerator: IEnumerator { <br /> int nIndex; <br /> Leafs collection; <br /> public LeafEnumerator(Leafs coll) { <br /> collection = coll; <br /> nIndex = -1; <br /> }<br /> // Sets the enumerator to its initial position, which is<br /> // before the first element in the collection.<br /> public void Reset() { <br /> nIndex = -1;<br /> }<br /> // Advances the enumerator to the next element of the <br /> // collection. <br /> public bool MoveNext() {<br /> nIndex++; <br /> return(nIndex &lt; collection.Count); <br /> }<br /> // Gets the current element in the collection.<br /> public Leaf Current {<br /> get { <br /> return((Leaf)collection.Item(nIndex)); <br /> } <br /> } <br /> // The current property on the IEnumerator interface: <br /> object IEnumerator.Current { <br /> get { <br /> return(Current); <br /> } <br /> } <br />} // End of class LeafEnumeration<br /></span>
<p><span class="smallblack">As seen above our LeafEnumerator Classimplements the IEnumerator interface. So we have to provide theimplementation for the functions Reset, MoveNext and Current. The classLeafEnumerator can exist as separate class in the same assembly or canbe inner class in our Leafs collection. It is best practice to have itas the inners class because it will hardly be used directly inapplication.<br />Now we will provide the base implementation for theIEnumerable.GetEnumerator() and a new implementation for the functionGetEnumerator() in the following manner</span></p>
<p><span class="smallblack">// Implement the method GetEnumerator from the interface <br />//IEnumerable <br />public new LeafEnumerator GetEnumerator() { <br /> return new LeafEnumerator(this); <br />} <br />// Implement the GetEnumerator() method: <br />IEnumerator IEnumerable.GetEnumerator() { <br /> return GetEnumerator(); <br />}<br /></span>
<p><span class="smallblack">And we are done. Now let us test our Collection class. In the following code we <br />?	Create the instance of the collection class.<br />?	Adds the Leafs items to the collection<br />?	Iterates through the collection Items and displays the output on to the console. </span></p>
<p><span class="smallblack">class Entry{ <br /> public static void Main() { <br /> Leafs lfc = new Leafs(); <br /> Leaf lf = new Leaf(); <br /> lf.Age = 10; <br /> lf.Name = &quot;Paresh&quot;; <br /> Leaf lf1 = new Leaf(); <br /> lf1.Age = 100; <br /> lf1.Name = &quot;Sharmili&quot;; <br /> lfc.Add(lf); <br /> lfc.Add(lf1); <br /> foreach(Leaf l in lfc) {<br /> Console.WriteLine(&quot;Name : &quot; + l.Name + &quot; Age : &quot; + l.Age); <br /> } <br /> } <br />}<br /></span>
<p><span class="smallblack">Output displayed at the console:Name : Paresh Age : 10Name : Sharmili Age : 100</span></p>
<p><span class="smallblack">It is always a good practice to usecollections over arrays when dealing with the reference data types. Itavoids most of the runtime error that can occur when using the arrays.Also it relives the programmer from tracking for the array index beinginvalid. At the same time it makes the code look cleaner and morereadable.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2006/07/developing-collection-classes-in-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Collections</title>
		<link>http://www.csharphelp.com/2006/05/c-collections/</link>
		<comments>http://www.csharphelp.com/2006/05/c-collections/#comments</comments>
		<pubDate>Sun, 14 May 2006 04:01:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Collections]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com.php5-3.dfw1-2.websitetestlink.com/?p=178</guid>
		<description><![CDATA[To construct and manipulate a collection of objects, .Net framework has provided us with many classes. ArrayList, BitArray, HashTable, SortedList, Queue and Stack are some of them. They are all included in System.Collections namespace. Download myQueue.cs Download myStack.cs In general, Arrays are not dynamic in nature and do not allow the user to remove an [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size: xx-small;"><strong><br />
</strong></span><span class="smallblack"><a href="mailto:vijay_patil@hotmail.com"></a></span></p>
<p><span class="smallblack">To construct and manipulate a collection of objects, .Net framework has provided us with many classes. ArrayList, BitArray, HashTable, SortedList, Queue and Stack are some of them. They are all included in System.Collections namespace.</span></p>
<p><span class="smallblack">Download <a href="http://www.csharphelp.com/archives/files/archive183/myQueue.cs">myQueue.cs</a></span></p>
<p><span class="smallblack">Download <a href="http://www.csharphelp.com/archives/files/archive183/myStack.cs">myStack.cs</a></span></p>
<p><span class="smallblack">In general, Arrays are not dynamic in nature and do not allow the user to remove an element easily. An ArrayList is like an expandable array of objects. Its Add, Insert, Remove RemoveAt methods make it relatively easy to insert and remove elements from the ArrayList. But we cannot refer to an entry in an ArrayList by a key. HastTable solves this problem but it does not allow its entries to be addressed using indexes. It also does not allow us to retrieve the entries in any particular order. SortedList represents a collection of associated keys &amp; values. The values are sorted by key and are accessible by key and by Index. </span></p>
<p><span class="smallblack">The purpose of this paper is to demonstrate the use of stack and Queue classes, identify their key strengths and limitations. This paper first explains the stack/queue concept and then applies the concept to an example. The program is explained with the help of comment lines.</span></p>
<p><span class="smallblack">STACK </span></p>
<p><span class="smallblack">Stack class is generally referred to as LIFO (Last In First Out). The best example could be &#8220;a stack of plates in the fabrication company&#8221;. The last plate placed on the stack is the first one to be removed. Stack in implemented as circular buffer. The various methods of stack class are</span></p>
<p><span class="smallestblack"><span class="smallblack">Push<br />
Inserts an object at the top of the Stack</span></span></p>
<p>Pop<br />
Returns and permanently removes the object at the top of the Stack</p>
<p>Peek<br />
Returns the object at the top of the Stack without removing it</p>
<p>Clear<br />
Clears the stack by removing all objects from the Stack</p>
<p>Clone<br />
Creates a shallow copy of the Stack</p>
<p>CopyTo<br />
Copies the Stack to an existing one-dimensional Array</p>
<p>ToArray<br />
Copies the Stack to a new array</p>
<p>Contains<br />
Determines if an element is present in the Stack</p>
<p>Equals<br />
Determines if two Object instances are equal</p>
<p>ToString<br />
Returns a String that represents the current Object</p>
<p><span class="smallblack">GetEnumerator, GetHashCode and GetType are also some of the methods of stack class.<br />
Count, IsReadOnly, IsSynchronized, SyncRoot are the properties of Stack class.</span></p>
<p><span class="smallblack">The initial capacity is the starting capacity of the new stack. Once current capacity is reached, the capacity is doubled. The default initial capacity is 10. Almost all the methods of Stack class, except Synchronized method are virtual and can be overridden by the derived class. Stack is not thread-safe, To guarantee thread safety of a stack, all operations must be done through the wrapper returned by the Synchronized method.</span></p>
<p><span class="smallblack">Example using Queue</span></p>
<p><span class="smallblack"> <img src="http://www.csharphelp.com/archives/files/archive183/pic1.gif" alt="" /></span></p>
<p><span class="smallblack">The Code </span></p>
<p><span class="smallestblack"><span class="smallblack">// This example uses all the important methods of a stack class<br />
using System;<br />
using System.Collections;<br />
public class myStack<br />
{<br />
public static void Main()<br />
{ 	// Create and instantiate new Stack<br />
Stack myStack = new Stack();<br />
// Adding 4 objects to the stack<br />
myStack.Push(&#8220;Vijay&#8221;);<br />
myStack.Push(&#8220;Vikas&#8221;);<br />
myStack.Push(&#8220;Emill&#8221;);<br />
myStack.Push(&#8220;Rajiv&#8221;);<br />
// Create and initialize one-dimensional Array<br />
Array myArray=Array.CreateInstance(typeof(String),15);<br />
// Add 5 elements to the array<br />
myArray.SetValue(&#8220;student1&#8243;, 0);<br />
myArray.SetValue(&#8220;student2&#8243;, 1);<br />
myArray.SetValue(&#8220;student3&#8243;, 2);<br />
myArray.SetValue(&#8220;student4&#8243;, 3);<br />
myArray.SetValue(&#8220;student5&#8243;, 4);<br />
// Count the number of objects in the stack<br />
Console.Write(&#8220;Total number of objects in the stack : &#8221; );<br />
Console.Write(myStack.Count);<br />
// Print all the objects in the stack<br />
Console.Write(&#8220;\nThe objects are :&#8221;);<br />
PrintValues(myStack);<br />
Console.WriteLine();<br />
Console.WriteLine(&#8220;\tObject no \t3\t2\t1\t0&#8243;);<br />
// Remove an element from the stack (Last In First Out)<br />
Console.WriteLine();<br />
Console.WriteLine(&#8220;Pop removes&amp;shows Last element of the stack first&#8221;);<br />
Console.Write(&#8220;The element removed is : &#8221; );<br />
Console.Write(&#8220;\t{0}&#8221;, myStack.Pop());<br />
// Show an element from the stack<br />
Console.WriteLine(&#8220;\nPeek method just shows an element but does not<br />
remove it from the stack&#8221;);<br />
Console.Write(&#8220;The element shown is : &#8221; );<br />
Console.Write(&#8220;\t{0}&#8221;, myStack.Peek());<br />
Console.WriteLine();<br />
// Count and print the number of objects in the stack<br />
Console.Write(&#8220;\nTotal remaining number of objects in the stack : &#8221; );<br />
Console.Write(myStack.Count);<br />
Console.WriteLine();<br />
Console.Write(&#8220;\nThe objects are :&#8221;);<br />
PrintValues(myStack);<br />
Console.WriteLine();<br />
Console.WriteLine(&#8220;\tObject no \t2\t1\t0&#8243;);;<br />
// Clear the Stack.<br />
myStack.Clear();<br />
Console.WriteLine();<br />
Console.Write(&#8220;The stack is cleared. Now, total number of objects = &#8220;);<br />
Console.Write(myStack.Count);<br />
Console.WriteLine();<br />
// Refill the stack with different values<br />
myStack.Push(&#8220;Patil&#8221;);<br />
myStack.Push(&#8220;Agarwal&#8221;);<br />
myStack.Push(&#8220;Tsankov&#8221;);<br />
myStack.Push(&#8220;Gomes&#8221;);<br />
myStack.Push(&#8220;Smith&#8221;);<br />
// Count and print the number of objects in the stack<br />
Console.WriteLine();<br />
Console.Write(&#8220;Total number of NEW objects in the stack : &#8221; );<br />
Console.Write(myStack.Count);<br />
Console.WriteLine();<br />
Console.Write(&#8220;\nThe objects are :&#8221;);<br />
PrintValues(myStack);<br />
Console.WriteLine();<br />
Console.WriteLine(&#8220;\tObject no \t4\t3\t2\t1\t0&#8243;);<br />
Console.WriteLine();<br />
// Display the values of the target Array instance.<br />
Console.WriteLine(&#8220;The target Array contains following elements BEFORE<br />
copying):&#8221; );<br />
PrintValues(myArray,&#8217; &#8216;);<br />
// Copy the entire source Stack to target Array, starting at index 3.<br />
myStack.CopyTo( myArray, 3 );<br />
Console.WriteLine();<br />
Console.WriteLine(&#8220;The target Array contains following elements AFTER<br />
copying):&#8221; );<br />
PrintValues(myArray,&#8217; &#8216;);<br />
}</span></span><br /><i>Continues&#8230;</i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2006/05/c-collections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Custom Collection Implementation</title>
		<link>http://www.csharphelp.com/2006/02/c-custom-collection-implementation/</link>
		<comments>http://www.csharphelp.com/2006/02/c-custom-collection-implementation/#comments</comments>
		<pubDate>Mon, 13 Feb 2006 04:01:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Collections]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com.php5-3.dfw1-2.websitetestlink.com/?p=88</guid>
		<description><![CDATA[This sample shows a simple custom collection implementation in C# with the .NET Framework. using System;using System.Collections; namespace Personal.Demos.Simple{ public class MainApp{ //entry point &#39;Main&#39; public static void Main(){ //get us some Widgets Widgets MyWidgets = Initialize(); Console.WriteLine(&#34;Using &#39;foreach&#39; &#8211; &#34;); //enumerate through the collection with //the foreach constuct. foreach(Widget W in MyWidgets){ //call the [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:xx-small;"><b><br /></b></span><span class="smallblack"><a href="http://www.csharphelp.com/bio/tollington.html"></a></span></p>
<p><span class="smallblack">This sample shows a simple custom collection implementation in C# with the .NET Framework.</span></p>
<p><span class="smallblack">using System;<br />using System.Collections;</p>
<p>namespace Personal.Demos.Simple{</p>
<p> public class MainApp{</p>
<p> //entry point &#39;Main&#39;</p>
<p> public static void Main(){</p>
<p> //get us some Widgets<br /> Widgets MyWidgets = Initialize(); </p>
<p> Console.WriteLine(&quot;Using &#39;foreach&#39; &#8211; &quot;);</p>
<p> //enumerate through the collection with<br /> //the foreach constuct.</p>
<p> foreach(Widget W in MyWidgets){<br /> //call the same method on each item in<br /> //the collection.<br /> Console.WriteLine(W.AMethod());<br /> }</p>
<p> Console.WriteLine(&quot;Using &#39;Indexers&#39; &#8211; &quot;);</p>
<p> //enumerate through the collection with<br /> //it&#39;s indexer.</p>
<p> for(int i=0; i &lt; MyWidgets.Count ; i++){<br /> //call the same method on each item in<br /> //the collection.<br /> Console.WriteLine(MyWidgets[i].AMethod());<br /> }</p>
<p> }</p>
<p> //fill up a &#39;Widgets&#39; collection with&#8230;<br /> //&#8230;well some Widgets.</p>
<p> private static Widgets Initialize(){</p>
<p> Widgets colWidgets = new Widgets();<br /> Widget objWidget;<br /> int i;</p>
<p> for(i=0 ; i &lt; 10 ; i++){<br /> objWidget = new Widget();<br /> objWidget.Number=i;<br /> colWidgets.Add(objWidget);<br /> }</p>
<p> return colWidgets;<br /> }</p>
<p> }</p>
<p> //*****************************************************<br /> //the individual Widget class</p>
<p> public class Widget{</p>
<p> public int Number=0;</p>
<p> public virtual string AMethod(){</p>
<p> return &quot;This Widget&#39;s Number is : &quot; <br /> + Number.ToString();</p>
<p> }<br /> }</p>
<p> //*****************************************************<br /> //Widgets collection class MUST derive from <br /> //System.Collections.CollectionBase</p>
<p> public class Widgets:CollectionBase{</p>
<p> public virtual void Add(Widget NewWidget){<br /> //forward our Add method on to <br /> //CollectionBase.IList.Add <br /> this.List.Add(NewWidget);</p>
<p> }</p>
<p> //this is the indexer (readonly)<br /> public virtual Widget this[int Index]{</p>
<p> get{<br /> //return the Widget at IList[Index]<br /> return (Widget)this.List[Index];</p>
<p> }</p>
<p> }<br /> }<br /> //*****************************************************<br />}<br /></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2006/02/c-custom-collection-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

