<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>josemiguel.torres</title>
	
	<link>http://josemigueltorres.net/boresight</link>
	<description>Off-Boresight .NET Technologies</description>
	<lastBuildDate>Thu, 02 Sep 2010 07:00:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/josemigueltorres/rXsu" /><feedburner:info uri="josemigueltorres/rxsu" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>[Whitepaper gratuito] Comenzando con Open Data Protocol</title>
		<link>http://feedproxy.google.com/~r/josemigueltorres/rXsu/~3/HR8NSSmQLwc/</link>
		<comments>http://josemigueltorres.net/boresight/?p=419#comments</comments>
		<pubDate>Thu, 02 Sep 2010 07:00:53 +0000</pubDate>
		<dc:creator>josemiguel.torres</dc:creator>
				<category><![CDATA[Publicaciones]]></category>
		<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[OData]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Windows Phone]]></category>

		<guid isPermaLink="false">http://josemigueltorres.net/boresight/?p=419</guid>
		<description><![CDATA[En el MIX de Las Vegas, ví las primeras demos y características de Open Data Protocol –OData- de la mano de nada más y nada menos que de Pablo Castro y quede gratamente sorprendido pues redireccionaban el camino equivocado tomado hacia unos meses con Astoria. La verdad es que la idea inicial de Astoria era [...]]]></description>
			<content:encoded><![CDATA[<p><img style="display: inline; margin-left: 0px; margin-right: 0px" align="right" src="http://t1.gstatic.com/images?q=tbn:ANd9GcQNbIqm1AMeJA8NNWalO7TFxMyvnDyKFSQp_oNItgsRwglyVVE&amp;t=1&amp;usg=__SXt-pkLTFcb4970ddUVvB5P7tgo=" />En el MIX de Las Vegas, ví las primeras demos y características de <strong>Open Data Protocol </strong>–<strong>OData</strong>- de la mano de nada más y nada menos que de <strong>Pablo Castro </strong>y quede gratamente sorprendido pues redireccionaban el camino equivocado tomado hacia unos meses con Astoria. La verdad es que la idea inicial de Astoria era algo alentadora, pero no la puestra en práctica. La coletilla o pretexto de que OData era una evolución de Astoria, no decia mucho a su favor, en principio, pero despues de haberlo puesto en práctica en algunos determinados escenarios en mi actual empresa, estoy francamente satisfecho.</p>
<p>Si bien es cierto que la idea subyacente de Astoria prometía, a la práctica no fue así. Los orígenes de datos expuestos eran propietarios. No puedo admitir que fuera ni un experto ni un fan, tanto de EF como de Astoria, pero lo cierto es que el poco contacto q tuve con ambas tecnologias simultáneamente admito que la experiencia no fue positiva.</p>
<p>Entonces, ¿Que ha cambiado con OData? En primer lugar, <strong>Microsoft</strong> anuncia a través de <a href="http://www.microsoft.com/Interop/osp/default.mspx" target="_blank"><strong>Microsoft’s Open Specification Promise</strong></a> (OSP) la estandarización de la tecnología. En segundo lugar tratar de dar solución a la exposición de orígenes de datos, relacionales o no, a través de un Servicio Web ejecutando operaciones con un lenguaje “estándar” basandose en:</p>
<ul>
<li>La creación de una forma uniforme de representación de datos estructurados a través de <strong>Atom</strong> o <strong>JSON</strong> (JavaScript Object Notation) </li>
<li>La utilización de convenciones URL uniformes tanto para la navegación, filtrado, orden y paginación de datos (entre otros) </li>
<li>La creación de operaciones uniformes dirigidas por dirección mediante las acciones GET, POST, PUT y DELETE. </li>
</ul>
<p>En definitiva no OData no es más que una estandarización de la creación y consumo de datos via Web que está construido sobre un conjunto de estándares de Internet, espcialmente <a href="http://en.wikipedia.org/wiki/Atom_(standard)" target="_blank">Atom Publishing Protocol</a><strong> </strong>o AtomPub especificado en <strong>RFD 5023</strong> quien a su vez está sontruido sobre <strong>Atom Syndication Format</strong> o Atom especificado en RFC4287.</p>
<p>En este whitepaper he tratado de introducir la problemática y necesidades de la exposición de orígenes de datos con las tecnologias Web actuales y cómo OData soluciona parte de esas problemáticas. OData ofrece una gran variedad de operaciones sobre un conjunto de datos que nos aportan una mayor flexibilidad y optimización y por otra parte una alta integración con todo tipo de clientes:</p>
<ul>
<li>Cualquier explorador Web </li>
<li>Apps .NET Framework 3.5/4.0 </li>
<li>Silverlight 3/4 </li>
<li>Windows Phone 7 </li>
<li>Java / JavaScript </li>
<li>PHP </li>
<li>AJAX </li>
<li>Excel 2010 PowerPoint </li>
<li>LINQPad </li>
<li>SQL Azure </li>
</ul>
<p>Además de mostrar cómo crear un servicio OData muestro varios ejemplos básicos sobre algunos de los cliente enumerados arriba. En definitiva, un Whitepaper que pretende presentar de una forma práctica <strong>Open Data Protocol</strong> a cualquier desarrollador y empezar a valorar la utilidad de esta nueva apuesta para nuestros desarrollos.</p>
<h2>Recursos</h2>
<p>- <a href="http://shop.campusmvp.com/Product-Whitepaper-Comenzando-con-OData-(PDF)_94.aspx" target="_blank">PDF/Libro electrónico</a></p>
<p>- <a href="http://www.scribd.com/doc/36727832/Whitepaper-Comenzando-Con-OData-JM-Torres-Krasis-Press" target="_blank">Lectura OnLine</a></p>
<p>- <a href="http://www.facebook.com/#!/campusMVP?ref=ts" target="_blank">Sigue los comentarios en la página de Facebook de CampusMVP</a></p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://josemigueltorres.net/boresight/?p=419&amp;feed=comments-rss2" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://josemigueltorres.net/boresight/?p=419&amp;title=%5BWhitepaper+gratuito%5D+Comenzando+con+Open+Data+Protocol" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://josemigueltorres.net/boresight/?p=419&amp;title=%5BWhitepaper+gratuito%5D+Comenzando+con+Open+Data+Protocol" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://josemigueltorres.net/boresight/?p=419&amp;title=%5BWhitepaper+gratuito%5D+Comenzando+con+Open+Data+Protocol&amp;desc=En%20el%20MIX%20de%20Las%20Vegas%2C%20v%C3%AD%20las%20primeras%20demos%20y%20caracter%C3%ADsticas%20de%20Open%20Data%20Protocol%20%E2%80%93OData-%20de%20la%20mano%20de%20nada%20m%C3%A1s%20y%20nada%20menos%20que%20de%20Pablo%20Castro%20y%20quede%20gratamente%20sorprendido%20pues%20redireccionaban%20el%20camino%20equivocado%20tomado%20hacia%20unos%20meses%20con%20Astoria.%20La%20verdad%20es%20que%20la%20idea%20inicial%20de" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://josemigueltorres.net/boresight/?p=419&amp;t=%5BWhitepaper+gratuito%5D+Comenzando+con+Open+Data+Protocol" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://josemigueltorres.net/boresight/?p=419&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://josemigueltorres.net/boresight/?p=419&amp;title=%5BWhitepaper+gratuito%5D+Comenzando+con+Open+Data+Protocol&amp;summary=En%20el%20MIX%20de%20Las%20Vegas%2C%20v%C3%AD%20las%20primeras%20demos%20y%20caracter%C3%ADsticas%20de%20Open%20Data%20Protocol%20%E2%80%93OData-%20de%20la%20mano%20de%20nada%20m%C3%A1s%20y%20nada%20menos%20que%20de%20Pablo%20Castro%20y%20quede%20gratamente%20sorprendido%20pues%20redireccionaban%20el%20camino%20equivocado%20tomado%20hacia%20unos%20meses%20con%20Astoria.%20La%20verdad%20es%20que%20la%20idea%20inicial%20de&amp;source=josemiguel.torres" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://josemigueltorres.net/boresight/?p=419&amp;bm_description=%5BWhitepaper+gratuito%5D+Comenzando+con+Open+Data+Protocol&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://josemigueltorres.net/boresight/?p=419&amp;title=%5BWhitepaper+gratuito%5D+Comenzando+con+Open+Data+Protocol" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://josemigueltorres.net/boresight/?p=419&amp;title=%5BWhitepaper+gratuito%5D+Comenzando+con+Open+Data+Protocol" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://josemigueltorres.net/boresight/?p=419&amp;title=%5BWhitepaper+gratuito%5D+Comenzando+con+Open+Data+Protocol" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://josemigueltorres.net/boresight/?p=419" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=%5BWhitepaper+gratuito%5D+Comenzando+con+Open+Data+Protocol+-+http://b2l.me/anvg5t&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-yahoomail">
			<a href="http://compose.mail.yahoo.com/?Subject=%5BWhitepaper+gratuito%5D+Comenzando+con+Open+Data+Protocol&amp;body=Link: http://josemigueltorres.net/boresight/?p=419 (sent via shareaholic)%0D%0A%0D%0A----%0D%0A En%20el%20MIX%20de%20Las%20Vegas%2C%20v%C3%AD%20las%20primeras%20demos%20y%20caracter%C3%ADsticas%20de%20Open%20Data%20Protocol%20%E2%80%93OData-%20de%20la%20mano%20de%20nada%20m%C3%A1s%20y%20nada%20menos%20que%20de%20Pablo%20Castro%20y%20quede%20gratamente%20sorprendido%20pues%20redireccionaban%20el%20camino%20equivocado%20tomado%20hacia%20unos%20meses%20con%20Astoria.%20La%20verdad%20es%20que%20la%20idea%20inicial%20de" rel="nofollow" class="external" title="Email this via Yahoo! Mail">Email this via Yahoo! Mail</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://josemigueltorres.net/boresight/?feed=rss2&amp;p=419</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://josemigueltorres.net/boresight/?p=419</feedburner:origLink></item>
		<item>
		<title>-Colecciones del ObjectModel- Coleccionando objetos en .NET Framework (III)</title>
		<link>http://feedproxy.google.com/~r/josemigueltorres/rXsu/~3/b2lc8_SU6pc/</link>
		<comments>http://josemigueltorres.net/boresight/?p=415#comments</comments>
		<pubDate>Sat, 17 Jul 2010 17:02:32 +0000</pubDate>
		<dc:creator>josemiguel.torres</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[System.Collections]]></category>
		<category><![CDATA[Visual C#]]></category>

		<guid isPermaLink="false">http://josemigueltorres.net/boresight/?p=415</guid>
		<description><![CDATA[Introducción Esta serie de artículos pretende mostrar las características y peculiaridades de las colecciones en .NET. El objetivo no es mostrar las situaciones en las que cada una de los diferentes tipos de colecciones pueden utilizarse en cada contexto sino la de conocer las mismas, ventajas y desventajas, para poder seleccionar el tipo de colección [...]]]></description>
			<content:encoded><![CDATA[<h1>Introducción</h1>
<p>Esta serie de artículos pretende mostrar las características y peculiaridades de las colecciones en .NET. El objetivo no es mostrar las situaciones en las que cada una de los diferentes tipos de colecciones pueden utilizarse en cada contexto sino la de conocer las mismas, ventajas y desventajas, para poder seleccionar el tipo de colección más apropiado en cada momento.</p>
<p>La serie cubre (entre corchetes el estado):</p>
<ul>
<li><a href="http://josemigueltorres.net/boresight/?p=404">Colecciones No Genericas</a> </li>
<li><a href="http://josemigueltorres.net/boresight/?p=411" target="_blank">Colecciones Genéricas System.Collections.Generic</a> </li>
<li><strong>Colecciones de System.Collections.ObjectModel (este post)</strong> </li>
<li>Colecciones nuevas en CLR 4.0 (no es definitivo además solo hay una) </li>
<li>Cota superior asintótica de las colecciones del CLI. aka Notación de Landau &#8211; O Grande- (en desarrollo) </li>
<li>Power Collections y C5 (por ahí andan) </li>
<li>Rx&#160; (si hay ganas y tiempo) </li>
</ul>
<h1>Colecciones System.Collections.ObjectModel</h1>
<p>Todas las colecciones agrupadas bajo <strong>System.Collections.ObjectModel </strong>tiene como denominador común que todas y cada una de ellas pueden ser utilizadas como modelo de objetos en librerias públicas con la intención de ser extendidas.</p>
<p><strong>Krzysztof Cwalina </strong>expone sus razones por las cuales algunas de estas clases han sido clasificadas bajo dicho espacio de nombres <strong>en el siguiente </strong><a href="http://blogs.msdn.com/b/kcwalina/archive/2005/03/15/396086.aspx" target="_blank"><strong>post</strong></a>. Básicamente, la gran mayoria de desarrolladores nos conformaremos con utilizar las colecciones expuestas tanto en <strong>System.Collections </strong>como en <strong>System.Collections.Generics </strong>dejando las expuestas en <strong>ObjectModel </strong>las utilizadas en menor medidad para otros usos no tan habituales como es&#160; la generacción de colecciones propietarias.</p>
<p>Dicho esto, lo que pretende este post es introducir las 5 clases con las que se presenta .NET Framework 4.0 y que son:</p>
<ul>
<li>System.Collections.ObjectModel.Collection&lt;T&gt; </li>
<li>System.Collections.ObjectModel.KeyedCollection&lt;TKey,TItem&gt; </li>
<li>System.Collections.ObjectModel.ObservableCollection&lt;T&gt; </li>
<li>System.Collections.ObjectModel.ReadOnlyCollection&lt;T&gt; </li>
<li>System.Collections.ObjectModel.ReadOnlyObservableCollection&lt;T&gt; </li>
</ul>
<h3>System.Collections.ObjectModel.Collection&lt;T&gt;</h3>
<p><strong>Collection&lt;T&gt;</strong> se utiliza como clase base (pese a que no es abstracta) para la implementación de colecciones personalizadas. Para ello expone parte de sus métodos como virtuales, tales como <strong>ClearItems</strong>, <strong>InsertItem</strong>, <strong>RemoveItem </strong>y <strong>SetItem</strong>. Por lo tanto se recomienda que todo los desarroladores que pretendan generar una colección personalizada extiendan <strong>Collection&lt;T&gt;</strong> en lugar de crear una desde zero.</p>
<p>El propio <strong>.NET Framework </strong>hace un uso extensivo de Collection&lt;T&gt; en gran parte de las colecciones utilizadas:</p>
<p><em><strong>System.Object</strong>       <br /><strong>&#160; System.Collections.ObjectModel.Collection&lt;T&gt;</strong>       <br />&#160;&#160;&#160; System.Collections.ObjectModel.KeyedCollection&lt;TKey, TItem&gt;       <br />&#160;&#160;&#160; System.Collections.ObjectModel.ObservableCollection&lt;T&gt;       <br />&#160;&#160;&#160; System.ComponentModel.BindingList&lt;T&gt;       <br />&#160;&#160;&#160; System.ComponentModel.SortDescriptionCollection       <br />&#160;&#160;&#160; System.Net.IPEndPointCollection       <br />&#160;&#160;&#160; System.Net.Mail.AlternateViewCollection       <br />&#160;&#160;&#160; System.Net.Mail.AttachmentCollection       <br />&#160;&#160;&#160; System.Net.Mail.LinkedResourceCollection       <br />&#160;&#160;&#160; System.Net.Mail.MailAddressCollection       <br />&#160;&#160;&#160; System.Net.PeerToPeer.CloudCollection       <br />&#160;&#160;&#160; System.Net.PeerToPeer.Collaboration.PeerApplicationCollection       <br />&#160;&#160;&#160; System.Net.PeerToPeer.Collaboration.PeerContactCollection       <br />&#160;&#160;&#160; System.Net.PeerToPeer.Collaboration.PeerEndPointCollection       <br />&#160;&#160;&#160; System.Net.PeerToPeer.Collaboration.PeerNearMeCollection       <br />&#160;&#160;&#160; System.Net.PeerToPeer.Collaboration.PeerObjectCollection       <br />&#160;&#160;&#160; System.Net.PeerToPeer.PeerNameRecordCollection       <br />&#160;&#160;&#160; System.Security.Cryptography.CngPropertyCollection       <br />&#160;&#160;&#160; System.ServiceModel.Channels.BindingElementCollection       <br />&#160;&#160;&#160; System.ServiceModel.Channels.ChannelParameterCollection       <br />&#160;&#160;&#160; System.ServiceModel.Description.FaultDescriptionCollection       <br />&#160;&#160;&#160; System.ServiceModel.Description.MessageDescriptionCollection       <br />&#160;&#160;&#160; System.ServiceModel.Description.OperationDescriptionCollection       <br />&#160;&#160;&#160; System.ServiceModel.Description.PolicyAssertionCollection       <br />&#160;&#160;&#160; System.ServiceModel.Description.ServiceEndpointCollection       <br />&#160;&#160;&#160; System.ServiceModel.Dispatcher.MessageQueryCollection       <br />&#160;&#160;&#160; System.ServiceModel.Syndication.SyndicationElementExtensionCollection       <br />&#160;&#160;&#160; System.Web.DynamicData.DataControlReferenceCollection       <br />&#160;&#160;&#160; System.Web.Routing.RouteCollection       <br />&#160;&#160;&#160; System.Web.UI.DataVisualization.Charting.ChartElementCollection&lt;T&gt;       <br />&#160;&#160;&#160; System.Web.UI.ScriptReferenceCollection       <br />&#160;&#160;&#160; System.Web.UI.ServiceReferenceCollection       <br />&#160;&#160;&#160; System.Web.UI.UpdatePanelTriggerCollection       <br />&#160;&#160;&#160; System.Windows.ConditionCollection       <br />&#160;&#160;&#160; System.Windows.Forms.DataVisualization.Charting.ChartElementCollection&lt;T&gt;       <br />&#160;&#160;&#160; System.Windows.Forms.FileDialogCustomPlacesCollection       <br />&#160;&#160;&#160; System.Windows.Ink.StrokeCollection       <br />&#160;&#160;&#160; System.Windows.Input.StylusPlugIns.StylusPlugInCollection       <br />&#160;&#160;&#160; System.Windows.Input.StylusPointCollection       <br />&#160;&#160;&#160; System.Windows.Input.TouchPointCollection       <br />&#160;&#160;&#160; System.Windows.SetterBaseCollection       <br />&#160;&#160;&#160; System.Windows.TriggerCollection       <br />&#160;&#160;&#160; System.Workflow.ComponentModel.Compiler.ValidationErrorCollection</em></p>
<h3>System.Collections.ObjectModel.KeyedCollection&lt;TKey,TItem&gt; </h3>
<p>Se trata de una clases abstracta que como peculiaridad <strong>las claves estan incrustadas en sus valores</strong>. Tal y como se define en el <strong>MSDN Library</strong>, <strong>KeyedCollection&lt;TKey,TItem&gt; </strong>es un conjunto infinito de tipos abstractos ya que la implementación dependerá en gran medida de los tipos genericos representados por <strong>TKey </strong>y <strong>TItem</strong>.</p>
<p>La clase <strong>KeyedCollection&lt;TKey, TItem&gt;</strong> es un híbrido entre una colección basada en <strong>IList&lt;T&gt;</strong> y una colección basada en <strong>IDictionary&lt;TKey, TValue&gt; </strong>y por tanto puede ser accedida mediante índice y clave. La diferencia fundamental es que ésta no es una par clave/valor ya que el propio valor agrupa tanto el valor en sí como la clave. Su firma es:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">abstract</span> <span style="color: #0000ff">class</span> KeyedCollection&lt;TKey, TItem&gt; : Collection&lt;TItem&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> KeyedCollection();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> KeyedCollection(IEqualityComparer&lt;TKey&gt; comparer);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> KeyedCollection(IEqualityComparer&lt;TKey&gt; comparer, <span style="color: #0000ff">int</span> dictionaryCreationThreshold);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> IEqualityComparer&lt;TKey&gt; Comparer { get; }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> TItem <span style="color: #0000ff">this</span>[TKey key] { get; }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> IDictionary&lt;TKey, TItem&gt; Dictionary { get; }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> Contains(TKey key);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> Remove(TKey key);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> ChangeItemKey(TItem item, TKey newKey);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> ClearItems();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">abstract</span> TKey GetKeyForItem(TItem item);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> InsertItem(<span style="color: #0000ff">int</span> index, TItem item);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> RemoveItem(<span style="color: #0000ff">int</span> index);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> SetItem(<span style="color: #0000ff">int</span> index, TItem item);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
</p></div>
</div>
<p>Por ejemplo, <strong>KeyedCollection&lt;int, Empleado&gt; </strong>puede contener una clave “234449” que pertenece como propiedad al propio elemento <strong>Empleado </strong>(como por ejemplo el código del mismo) que se obtendría a través del método abstracto <strong>GetKeyForItem() </strong>de la siguiente forma:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Departamento : KeyedCollection&lt;<span style="color: #0000ff">int</span>, Empleado&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> Departamento () : <span style="color: #0000ff">base</span>(<span style="color: #0000ff">null</span>, 0) {}</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span> GetKeyForItem(Empleado empleado)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">return</span> empleado.Codigo;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">     <span style="color: #008000">//Clase incompleta</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
</p></div>
</div>
<p>&#160;</p>
<h3>System.Collections.ObjectModel.ObservableCollection&lt;T&gt; </h3>
<p>&#160;<a title="http://msdn.microsoft.com/en-us/library/ms668604.aspx" href="http://msdn.microsoft.com/en-us/library/ms668604.aspx">http://msdn.microsoft.com/en-us/library/ms668604.aspx</a><strong>&#160;</strong></p>
<p>Las colecciones <strong><a href=" http://msdn.microsoft.com/en-us/library/ms668604.aspx " target="_blank">ObservableCollection&lt;T&gt;</a></strong> fueron una de las caracterísitcas más importantes introducidas a raíz de WPF y Silverlight posteriormente debido a la capacidad que tiene de propagar una modificación sobre el conjunto de elementos de la colección, a través de un par de interfaces que veremos a continuación, y que sirve como base para el Data Binding con los elementos o vistas WPF/Silverlight, por ejemplo.</p>
<p>Si observamos la firma de ObservableCollection&lt;T&gt; vemos que además de implementar la clase abstracta <strong>Collection&lt;T&gt;,</strong> implementa <strong>INotifyCollectionChanged</strong> e <strong>INotifyPropertyChanged</strong>.</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">[Serializable]</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ObservableCollection&lt;T&gt; </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    : Collection&lt;T&gt;, INotifyCollectionChanged, INotifyPropertyChanged</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">...</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
</p></div>
</div>
<p>Si vemos la definición de <strong>INotifyCollectionChanged:</strong></p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> INotifyCollectionChanged</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">event</span> NotifyCollectionChangedEventHandler CollectionChanged;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
</p></div>
</div>
<p>Vemos que implementa un evento del tipo <strong>NotifyCollectionChangedEventHandler </strong>el cual notificará a los “subscriptores” – o listeners en inglés- ante cualquier modificación sobre el conjunto de elementos. Ojo, se utiliza la palabra subscriptores entre comillas pues no es una entorno publicador – subscriptor real. (Véase <a href="http://josemigueltorres.net/boresight/?p=369" target="_blank"><strong>Observer Design Pattern with .NET</strong></a>)</p>
<p>Por otro lado, la interfaz <strong>INotifyPropertyChanged</strong>:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> INotifyPropertyChanged</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">event</span> PropertyChangedEventHandler PropertyChanged;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
</p></div>
</div>
<p>Es la que notificará las modificaciones, no sobre el conjunto de elementos, sino sobre los datos –Propiedades- de cualquier elemento de la colección. Las interfaces WPF/Silverlight basados en Data Bindings son los típicos clientes de esta interfaz y son los que representan la modificación de cualquier propiedad de cualquier elemento de una colección <strong>ObservableCollection&lt;T&gt;</strong> en el elemento enlazado de la interfaz.</p>
<p>Por ejemplo supongamos que tenemos la clase Empleado que pertenecerá a una ObservableCollection&lt;T&gt; y que posteriormente será enlazada a la interfaz de una aplicación WPF. En primer lugar debemos implementar la interfaz INotifyPropertyChanged para proveer de un mecanismo de notificación sobre las propiedades de la clase. Así, si la clase Empleado tiene dos propiedades llamadas Login y Nombre en ambos <em>setters </em>de la propiedad deberíamos ejecutar el evento PropertyChanged() tal y como se muestra a continuación:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Empleado : INotifyPropertyChanged</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">event</span> PropertyChangedEventHandler PropertyChanged;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> _login;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> _nombre;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Login</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            get { <span style="color: #0000ff">return</span> _login; }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            set</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                _login = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                PropertyChanged(<span style="color: #0000ff">this</span>, </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                    <span style="color: #0000ff">new</span> PropertyChangedEventArgs(<span style="color: #006080">&quot;Login&quot;</span>));</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Nombre</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            get { <span style="color: #0000ff">return</span> _nombre; }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            set</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                _nombre = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                PropertyChanged(<span style="color: #0000ff">this</span>, </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                    <span style="color: #0000ff">new</span> PropertyChangedEventArgs(<span style="color: #006080">&quot;Nombre&quot;</span>));</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
</p></div>
</div>
<p>A partir de aquí, la clase Empleado debe residir dentro de una colección ObservableCollection&lt;Empleado&gt; y ser enlazada a un elemento cualquier, por ejemplo un datagrid. Una ejemplo trivial con una única grid y un boton que agregaría un nuevo empleado a la colección seria:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> MainWindow : Window</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> ObservableCollection&lt;Empleado&gt; users </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        = <span style="color: #0000ff">new</span> ObservableCollection&lt;Empleado&gt;();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> MainWindow()</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        InitializeComponent();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        datagrid1.DataContext = users;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> button1_Click(<span style="color: #0000ff">object</span> sender, RoutedEventArgs e)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        users.Add(<span style="color: #0000ff">new</span> Empleado()</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            Login = <span style="color: #006080">&quot;jmtorres&quot;</span>,</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            Nombre = <span style="color: #006080">&quot;José Miguel&quot;</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        });</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
</p></div>
</div>
<p>En el XAML únicamente deberiamos hacer uso de los Data Bindings de WPF:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">DataGrid</span> <span style="color: #ff0000">Name</span><span style="color: #0000ff">=&quot;dataGrid1&quot;</span> <span style="color: #ff0000">ItemsSource</span><span style="color: #0000ff">=&quot;{Binding}&quot;</span> <span style="color: #ff0000">AutoGenerateColumns</span><span style="color: #0000ff">=&quot;False&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">&lt;</span><span style="color: #800000">DataGrid.Columns</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">&lt;</span><span style="color: #800000">DataGridTextColumn</span> <span style="color: #ff0000">Binding</span><span style="color: #0000ff">=&quot;{Binding Login}&quot;</span> <span style="color: #ff0000">Header</span><span style="color: #0000ff">=&quot;Login&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">&lt;</span><span style="color: #800000">DataGridTextColumn</span> <span style="color: #ff0000">Binding</span><span style="color: #0000ff">=&quot;{Binding Nombre}&quot;</span> <span style="color: #ff0000">Header</span><span style="color: #0000ff">=&quot;Nombre&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">DataGrid.Columns</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">DataGrid</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Canvas</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">&lt;</span><span style="color: #800000">Button</span> <span style="color: #ff0000">Click</span><span style="color: #0000ff">=&quot;button1_Click&quot;</span> <span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">Button</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Canvas</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span></pre>
</p></div>
</div>
<p>A continuación detallo enlaces que tratan de forma más específica los Data Bindings tanto en WPF como en Silverlight utilizando colecciones ObservableCollection&lt;T&gt;:</p>
<ul>
<h5><a href="http://sweux.com/blogs/psampaio/index.php/2009/04/28/an-introduction-to-observablecollection-in-wpf/">An Introduction to <em>ObservableCollection</em> in <em>WPF</em> | e-pedro.com</a></h5>
<h5><a href="http://www.codeproject.com/KB/WPF/ObservableCollection.aspx">Working with <em>ObservableCollection</em>&lt;T&gt; – CodeProject</a></h5>
<h5><a href="http://weblogs.asp.net/joelvarty/archive/2008/11/17/silverlight-databinding-the-observable-collection.aspx"><em>Silverlight</em> Databinding – The <em>Observable Collection</em> &#8211; Joel Varty</a></h5>
<h5><a href="http://blogs.msdn.com/b/msdnmagazine/archive/2008/11/28/9144980.aspx">Advanced Basics: The <em>ObservableCollection</em> Class &#8211; MSDN Magazine <b>&#8230;</b></a></h5>
</ul>
<ul></ul>
<ul></ul>
<p>&#160;</p>
<h3>System.Collections.ObjectModel.ReadOnlyCollection&lt;T&gt; </h3>
<p><strong>ReadOnlyCollection&lt;T&gt;</strong> es la versión Solo Lectura de la colección <strong>Collection&lt;T&gt;</strong>. Está especialmente para ser extendida en lugar de crear colecciones personalizadas desde zero y su peculiaridad, como su propio nombre indica es que <strong>carece de los 4 métodos que en la clase Collection&lt;T&gt;</strong> estaban declarados como virtuales y que se detallan a continuación:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">protected</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">void</span> ClearItems();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">protected</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">void</span> InsertItem(<span style="color: #0000ff">int</span> index, T item);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">protected</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">void</span> RemoveItem(<span style="color: #0000ff">int</span> index);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">protected</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">void</span> SetItem(<span style="color: #0000ff">int</span> index, T item);</pre>
</p></div>
</div>
<p>Por ultimo se detalla una relación de clases de .NET Framework que extienden las funcionalidades de <strong>ReadOnlyCollection&lt;T&gt;</strong></p>
<p><em><strong>System.Object<br />
      <br />&#160; System.Collections.ObjectModel.ReadOnlyCollection&lt;T&gt;</strong> </p>
<p>&#160;&#160;&#160; System.Collections.ObjectModel.ReadOnlyObservableCollection&lt;T&gt; </p>
<p>&#160;&#160;&#160; System.Data.Metadata.Edm.ReadOnlyMetadataCollection&lt;T&gt; </p>
<p>&#160;&#160;&#160; System.Security.Cryptography.ManifestSignatureInformationCollection </p>
<p>&#160;&#160;&#160; System.ServiceModel.Channels.AddressHeaderCollection </p>
<p>&#160;&#160;&#160; System.Windows.Input.StylusButtonCollection </p>
<p>&#160;&#160;&#160; System.Windows.Input.StylusDeviceCollection</em></p>
<h3>System.Collections.ObjectModel.ReadOnlyObservableCollection&lt;T&gt;</h3>
<p>La descripción oficial que hace el <strong>MSDN Library </strong>acerca de esta clase es que: “ReadOnlyObservableCollection es un wrapper –una envoltura- de sólo lectura sobre una colección ObservableCollection”. La percepción que tengo yo al respecto, siempre desde el respeto y la ignorancia especialmente sobre esta clase, es que no tengo ni <strike>pajolera</strike> idea de cual es su utilidad real llegando incluso a dudar de que realmente la tenga, con lo que no indagaré más al respecto.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://josemigueltorres.net/boresight/?p=415&amp;feed=comments-rss2" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://josemigueltorres.net/boresight/?p=415&amp;title=-Colecciones+del+ObjectModel-+Coleccionando+objetos+en+.NET+Framework+%28III%29" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://josemigueltorres.net/boresight/?p=415&amp;title=-Colecciones+del+ObjectModel-+Coleccionando+objetos+en+.NET+Framework+%28III%29" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://josemigueltorres.net/boresight/?p=415&amp;title=-Colecciones+del+ObjectModel-+Coleccionando+objetos+en+.NET+Framework+%28III%29&amp;desc=Introducci%C3%B3n%20%20Esta%20serie%20de%20art%C3%ADculos%20pretende%20mostrar%20las%20caracter%C3%ADsticas%20y%20peculiaridades%20de%20las%20colecciones%20en%20.NET.%20El%20objetivo%20no%20es%20mostrar%20las%20situaciones%20en%20las%20que%20cada%20una%20de%20los%20diferentes%20tipos%20de%20colecciones%20pueden%20utilizarse%20en%20cada%20contexto%20sino%20la%20de%20conocer%20las%20mismas%2C%20ventajas%20y" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://josemigueltorres.net/boresight/?p=415&amp;t=-Colecciones+del+ObjectModel-+Coleccionando+objetos+en+.NET+Framework+%28III%29" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://josemigueltorres.net/boresight/?p=415&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://josemigueltorres.net/boresight/?p=415&amp;title=-Colecciones+del+ObjectModel-+Coleccionando+objetos+en+.NET+Framework+%28III%29&amp;summary=Introducci%C3%B3n%20%20Esta%20serie%20de%20art%C3%ADculos%20pretende%20mostrar%20las%20caracter%C3%ADsticas%20y%20peculiaridades%20de%20las%20colecciones%20en%20.NET.%20El%20objetivo%20no%20es%20mostrar%20las%20situaciones%20en%20las%20que%20cada%20una%20de%20los%20diferentes%20tipos%20de%20colecciones%20pueden%20utilizarse%20en%20cada%20contexto%20sino%20la%20de%20conocer%20las%20mismas%2C%20ventajas%20y&amp;source=josemiguel.torres" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://josemigueltorres.net/boresight/?p=415&amp;bm_description=-Colecciones+del+ObjectModel-+Coleccionando+objetos+en+.NET+Framework+%28III%29&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://josemigueltorres.net/boresight/?p=415&amp;title=-Colecciones+del+ObjectModel-+Coleccionando+objetos+en+.NET+Framework+%28III%29" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://josemigueltorres.net/boresight/?p=415&amp;title=-Colecciones+del+ObjectModel-+Coleccionando+objetos+en+.NET+Framework+%28III%29" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://josemigueltorres.net/boresight/?p=415&amp;title=-Colecciones+del+ObjectModel-+Coleccionando+objetos+en+.NET+Framework+%28III%29" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://josemigueltorres.net/boresight/?p=415" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=-Colecciones+del+ObjectModel-+Coleccionando+objetos+en+.NET+Framework+%28III%29+-+http://b2l.me/abayn8&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-yahoomail">
			<a href="http://compose.mail.yahoo.com/?Subject=-Colecciones+del+ObjectModel-+Coleccionando+objetos+en+.NET+Framework+%28III%29&amp;body=Link: http://josemigueltorres.net/boresight/?p=415 (sent via shareaholic)%0D%0A%0D%0A----%0D%0A Introducci%C3%B3n%20%20Esta%20serie%20de%20art%C3%ADculos%20pretende%20mostrar%20las%20caracter%C3%ADsticas%20y%20peculiaridades%20de%20las%20colecciones%20en%20.NET.%20El%20objetivo%20no%20es%20mostrar%20las%20situaciones%20en%20las%20que%20cada%20una%20de%20los%20diferentes%20tipos%20de%20colecciones%20pueden%20utilizarse%20en%20cada%20contexto%20sino%20la%20de%20conocer%20las%20mismas%2C%20ventajas%20y" rel="nofollow" class="external" title="Email this via Yahoo! Mail">Email this via Yahoo! Mail</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://josemigueltorres.net/boresight/?feed=rss2&amp;p=415</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://josemigueltorres.net/boresight/?p=415</feedburner:origLink></item>
		<item>
		<title>-Colecciones Genericas- Coleccionando objetos en .NET Framework (II)</title>
		<link>http://feedproxy.google.com/~r/josemigueltorres/rXsu/~3/uImT5iE2-5s/</link>
		<comments>http://josemigueltorres.net/boresight/?p=411#comments</comments>
		<pubDate>Tue, 15 Jun 2010 17:11:58 +0000</pubDate>
		<dc:creator>josemiguel.torres</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[System.Collections]]></category>
		<category><![CDATA[Visual C#]]></category>

		<guid isPermaLink="false">http://josemigueltorres.net/boresight/?p=411</guid>
		<description><![CDATA[Introducción Esta serie de artículos pretende mostrar las características y peculiaridades de las colecciones en .NET. El objetivo no es mostrar las situaciones en las que cada una de los diferentes tipos de colecciones pueden utilizarse en cada contexto sino la de conocer las mismas, ventajas y desventajas, para poder seleccionar el tipo de colección [...]]]></description>
			<content:encoded><![CDATA[<h5></h5>
<h1>Introducción</h1>
<p>Esta serie de artículos pretende mostrar las características y peculiaridades de las colecciones en .NET. El objetivo no es mostrar las situaciones en las que cada una de los diferentes tipos de colecciones pueden utilizarse en cada contexto sino la de conocer las mismas, ventajas y desventajas, para poder seleccionar el tipo de colección más apropiado en cada momento.</p>
<p>La serie cubre (entre corchetes el estado):</p>
<ul>
<li><a href="http://josemigueltorres.net/boresight/?p=404" target="_blank">Colecciones No Genericas</a> </li>
<li><strong>Colecciones Genéricas System.Collections.Generic (este post) </strong></li>
<li>Colecciones de System.Collections.ObjectModel (en desarrollo) </li>
<li>Colecciones nuevas en CLR 4.0 (no es definitivo además solo hay una) </li>
<li>Cota superior asintótica de las colecciones del CLI. aka Notación de Landau &#8211; O Grande- (en desarrollo) </li>
<li>Power Collections y C5 (por ahí andan) </li>
<li>Rx&#160; (si hay ganas y tiempo) </li>
</ul>
<p><a href="http://msdn.microsoft.com/en-us/library/ms379570"></a></p>
<h1>Colecciones Genéricas</h1>
<p>Sin lugar a dudas, uno de los conceptos que más provecho ha sacado de la genericidad han sido las colecciones. Practicamente todas las colecciones utilizadas hasta entonces tienen su equivalente dentro del espacio de nombre <a href="http://msdn.microsoft.com/en-us/library/0sbxh9x2(v=VS.100).aspx" target="_blank">System.Collections.Generic</a><strong> </strong>además de la aparición de nuevas colecciones fruto de la flexibilidad que otorga los tipos generéricos. </p>
<p>En la siguiente tabla podemos ver algunas de estas colecciones (Fuente: Blog de <a href="http://blogs.msdn.com/b/kcwalina/archive/2005/09/23/collections.aspx" target="_blank">Krzysztof Cwalina</a> )</p>
<p><strong>No Genericas&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Similares de tipo Generico</strong>     <br />ArrayList&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; List&lt;T&gt;     <br />Hashtable&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Dictionary&lt;TKey,TValue&gt;     <br />SortedList&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SortedList&lt;TKey,TValue&gt;     <br />Queue&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Queue&lt;T&gt;     <br />Stack&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Stack&lt;T&gt;     <br />IEnumerable&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; IEnumerable&lt;T&gt;     <br />ICollection&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; N/A (use IEnumerable&lt;T&gt; anything that extends it)     <br />N/A&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ICollection&lt;T&gt;     <br />IList&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; IList&lt;T&gt;     <br />CollectionBase&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Collection&lt;T&gt;     <br />ReadOnlyCollectionBase&#160;&#160;&#160;&#160;&#160; ReadOnlyCollection&lt;T&gt;     <br />DictionaryBase&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; N/A (just implement IDictionary&lt;TKey,TValue&gt;     <br />N/A&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SortedDictionary&lt;TKey,TValue&gt;     <br />N/A&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; KeyedCollection&lt;TKey,TItem&gt;     <br />N/A&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LinkedList&lt;T&gt;</p>
<blockquote><p>NOTA: No todas las colecciones genéricas se encuentran en el espacio de nombres System.Collections.Generic. Algunas las veremos en el siguiente post bajo el espacio de nombre System.Collections.ObjectModel.</p>
</blockquote>
<p>De la misma forma que en la colecciones no genéricas, me gustaria empezar desgranando el núcleo de toda colección: el iterador/enumerador.</p>
<h2>El Enumerador genérico</h2>
<p>Como es de preveer existe una interfaz <strong><a href="http://msdn.microsoft.com/en-us/library/78dfe2yb(v=VS.100).aspx" target="_blank">IEnumerator&lt;T&gt;</a></strong> dentro del espacio de nombres <strong>System.Collections.Generic</strong> cuya firma es: </p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IEnumerator&lt;<span style="color: #0000ff">out</span> T&gt; : IDisposable, IEnumerator</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">new</span> T Current { get; }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
</p></div>
</div>
<p>Por consiguiente, la interfaz <strong><a href="http://msdn.microsoft.com/en-us/library/9eekhta0(v=VS.100).aspx" target="_blank">IEnumerable&lt;T&gt;</a></strong> también tiene su equivalente genérico y se describe como:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IEnumerable&lt;<span style="color: #0000ff">out</span> T&gt; : IEnumerable</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">new</span> IEnumerator&lt;T&gt; GetEnumerator();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
</p></div>
</div>
<blockquote>
<p>NOTA: La importancia y funcionamiento de ambas se describe en el post anterior. </p>
</blockquote>
<p>En dichas firmas ya empezamos a ver las primeras pecualiridades y es que en ambos casos cada una de las interfaces implementa su equivalente no genérico para mantener la compatibilidad hacía las colecciones no genéricas. Por otro lado <strong>IEnumerator&lt;T&gt;</strong> implementa <strong>IDisposable</strong>. La razón nos la explica el propio Krzysztof Cwalina <a href="http://blogs.msdn.com/b/kcwalina/archive/2005/01/07/348876.aspx" target="_blank">aqui</a> y no es más que para contemplar determinados escenarios dónde la colección que implementa <strong>IEnumerator&lt;T&gt;</strong> pueda destruir recursos “<em>externos</em>” utilizados tales como conexiones en base de datos para la lectura de filas o manejadores de archivos para la iteración de ficheros. Por último ambas interfaces son covariantes a T en el CLR 4.0. </p>
<p>Uno de los cambios más importantes es la abstracción de <strong><a href="http://msdn.microsoft.com/en-us/library/92t2ye13(v=VS.100).aspx" target="_blank">ICollection&lt;T&gt;</a></strong>. Si nos fijamos en la tabla de equivalencias del inicio de este post veremos que <strong>ICollection </strong>no tiene ninguna equivalencia en la parte genérica de la mismo forma que ICollection&lt;T&gt; tampoco la tiene en la parte no genérica. Fijémonos en su firma:</p>
<p><strong><a href="http://msdn.microsoft.com/en-us/library/92t2ye13(v=VS.100).aspx" target="_blank">ICollection&lt;T&gt;</a></strong></p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> ICollection&lt;T&gt; : IEnumerable&lt;T&gt;, IEnumerable</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">int</span> Count { get; }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">bool</span> IsReadOnly { get; }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">void</span> Add(T item);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">void</span> Clear();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">bool</span> Contains(T item);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">void</span> CopyTo(T[] array, <span style="color: #0000ff">int</span> arrayIndex);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">bool</span> Remove(T item);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
</p></div>
</div>
<p>Las unicas propiedades que tienen en común son <strong>CopyTo</strong> y <strong>Count</strong>. El método <strong>SyncRoot</strong> y la propiedad <strong>IsSynchronized</strong> se han eliminado y se han añadido los métodos <strong>Add</strong>, <strong>Contains</strong>, <strong>Clear </strong>y <strong>Remove</strong>. Queda evidente pues que lo único que comarten es el nombre ya que la abstracción es bien distinta.</p>
<blockquote>
<p>NOTA: <strong>Krzysztof Cwalina </strong>lo argumenta diciendo que en realidad <strong>ICollection </strong>no tenia mucho sentido. .NET Framework no tenia ningún tipo de colección que representara una colección indexada de lectura y escritura; de ahí surge <strong>ICollection&lt;T&gt;</strong>.</p>
</blockquote>
<p>En cuanto a las equivalencias de <strong>IList </strong>e <strong>IDictionary</strong>:</p>
<p><strong><a href="http://msdn.microsoft.com/en-us/library/5y536ey6(v=VS.100).aspx" target="_blank">IList&lt;T&gt;</a></strong></p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IList&lt;T&gt; : ICollection&lt;T&gt;, IEnumerable&lt;T&gt;, IEnumerable</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    T <span style="color: #0000ff">this</span>[<span style="color: #0000ff">int</span> index] { get; set; }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">int</span> IndexOf(T item);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">void</span> Insert(<span style="color: #0000ff">int</span> index, T item);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">void</span> RemoveAt(<span style="color: #0000ff">int</span> index);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
</p></div>
</div>
<p><strong><a href="http://msdn.microsoft.com/en-us/library/s4ys34ea(v=VS.100).aspx" target="_blank">IDictionary&lt;TKey,TValue&gt;</a></strong></p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IDictionary&lt;TKey, TValue&gt; : ICollection&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;,</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                             IEnumerable&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;, IEnumerable</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    TValue <span style="color: #0000ff">this</span>[TKey key] { get; set; }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    ICollection&lt;TKey&gt; Keys { get; }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    ICollection&lt;TValue&gt; Values { get; }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">bool</span> ContainsKey(TKey key);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">void</span> Add(TKey key, TValue <span style="color: #0000ff">value</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">bool</span> Remove(TKey key);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">bool</span> TryGetValue(TKey key, <span style="color: #0000ff">out</span> TValue <span style="color: #0000ff">value</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
</p></div>
</div>
<p>Podriamos afirmar, pues que ambas tienen su correspondencia en el espacio de nombre <strong>System.Collections.Generic</strong> pese a que <strong>IDictionary </strong>tiene algunas pecualiridades debido a la flexibilidad de los genéricos y que veremos más tarde. Por último siguen implementando la interfaz no genérica <strong>IEnumerable </strong>para mantener la compatibilidad con colecciones no genéricas. </p>
<p>Ahora veremos los diferentes tipos de colecciones en base a la interfaz que utilizan.</p>
<h1>Colecciones Genéricas</h1>
<h2>Colecciones regulares (System.Collections.ICollection)</h2>
<blockquote>
<p>NOTA: Pese a que <a href="http://msdn.microsoft.com/en-us/library/92t2ye13(v=VS.100).aspx" target="_blank">MSDN Library afirma que tanto Queue&lt;T&gt; y Stack&lt;T&gt; hacen uso de ICollection&lt;T&gt;</a> esto no es cierto. Implementan <strong>ICollection</strong>.</p>
</blockquote>
<p>Este tipo de colecciones también son conocidas como colecciones ordenadas o simplemente collecciones –me he permitido la libertad de denominarlas estándar o regulares para diferenciarlas de las específicas-. Hago hincapié en ello pues encontré varias formas de denomiarlas. De hecho, más allá de la colección Collection que como vimos anteriormente es abstracta y no es más que una evolución de <strong>CollectionBase</strong> utilizada en las versiones tempranas del CLR, existe un par de clases que podriamos catalogar dentro de este grupo y que vienen representadas por las clases <strong><a href="http://msdn.microsoft.com/en-us/library/x2bb46cs(v=VS.100).aspx" target="_blank">Stack&lt;T&gt;</a></strong> y <strong><a href="http://msdn.microsoft.com/en-us/library/1ttzy8a4(v=VS.100).aspx" target="_blank">Queue&lt;T&gt;</a></strong>. </p>
<blockquote>
<p>NOTA: Ambas guardan una estrecha relación ya que en ambos casos los elementos se almacenan según el orden de llegada. Nótese que el término “ordenación” utilizado anteriormente no es una ordenación lógica sobre una propiedad del conjunto de elemento; este tipo de elementos los veremos más adelante y podrían estar catalogados como colecciones especializadas.</p>
</blockquote>
<p>La diferencia principal entre <strong>Stack&lt;T&gt; </strong>y <strong>Queue&lt;T&gt; </strong>reside en el orden en el que los elementos son obtenidos. En ambos casos almacenan elementos en estricto orden de llegada y mientras que en la clase <strong>Stack&lt;T&gt; </strong>responde a LIFO (Last Input First Output) en el caso de <strong>Queue&lt;T&gt;</strong> responde a FIFO (First Input First Output). Ambas clases derivan de <strong>ICollection</strong> e <strong>IClonable </strong>y cada una de ellas utiliza un método distinto para añadir y quitar un elemento de la colección. <strong>Push </strong>y <strong>Pull </strong>para <strong>Stack </strong>y <strong>Enqueue </strong>y <strong>Dequeu </strong>para <strong>Queue</strong>.</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var queue = <span style="color: #0000ff">new</span> Queue&lt;<span style="color: #0000ff">string</span>&gt;();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var stack = <span style="color: #0000ff">new</span> Stack&lt;<span style="color: #0000ff">string</span>&gt;();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">queue.Enqueue(<span style="color: #006080">&quot;A&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">queue.Enqueue(<span style="color: #006080">&quot;B&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">queue.Enqueue(<span style="color: #006080">&quot;C&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">queue.Enqueue(<span style="color: #006080">&quot;D&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">stack.Push(<span style="color: #006080">&quot;A&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">stack.Push(<span style="color: #006080">&quot;B&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">stack.Push(<span style="color: #006080">&quot;C&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">stack.Push(<span style="color: #006080">&quot;D&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">Console.WriteLine(<span style="color: #006080">&quot;Dequeue-ing...&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">foreach</span> (var elemento <span style="color: #0000ff">in</span> queue)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Console.WriteLine(elemento);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">Console.WriteLine(<span style="color: #006080">&quot;Pop-ing...&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">foreach</span> (var elemento <span style="color: #0000ff">in</span> stack)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Console.WriteLine(elemento);</pre>
</p></div>
</div>
<h2>Colecciones (System.Collections.Generic.ICollection&lt;T&gt;)</h2>
<p><strong><a href="http://msdn.microsoft.com/en-us/library/2s4xk11f(v=VS.100).aspx" target="_blank">LinkedList&lt;T&gt;</a></strong> es una colección de nodos enlazados que implementa, entre otros <strong>ICollection&lt;T&gt;</strong>. El nucleo central de <strong>LinkedList&lt;T&gt;</strong> es <strong><a href="http://msdn.microsoft.com/en-us/library/ahf4c754(v=VS.100).aspx" target="_blank">System.Collections.Generic.LinkedListNode&lt;T&gt;</a></strong> la cual es una clase cuya caracteristica fundamental es que almacena el nodo immediatamente posterior y anterior y portanto todos y cada uno de los nodos mantienen una referencia de orden. Más info: <a href="http://en.wikipedia.org/wiki/Linked_list" target="_blank">Wikipedia</a>.</p>
<p>A partir de aquí el funcionamiento es relativamente sencillo. Lo que iremos almacenando son objetos del tipo <strong>LinkedListNode&lt;T&gt; </strong>de tipo T en una colección <strong>LinkedList&lt;T&gt;</strong>. Las ventajas que nos ofrece este tipo de listas que podemos insertar immediantamente antes o despues de un determinado nodo practicamente con un coste O(1)&#160; a través de los métodos <strong>AddBefore()</strong> y <strong>AddAfter()</strong>. Además podremos añadir un elemento al inicio o al final de la lista enlazada mediante <strong>AddFirst() </strong>y <strong>AddLast() </strong>respectivamente. </p>
<p><strong>LinkedList&lt;T&gt; </strong>mantiene siempre tanto el primer como el último nodo a través de las propiedades <strong>First </strong>y <strong>Last </strong>siempre y cuando el objecto <strong>LinkedList&lt;T&gt;</strong> tenga más de 0 elementos (linkedList.Count =&gt; 1).&#160; En el siguiente ejemplo, aunque trivial, muestra alguna de las posibilidades de <strong>LinkedList&lt;T&gt;</strong>.</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var linkedList = <span style="color: #0000ff">new</span> LinkedList&lt;<span style="color: #0000ff">string</span>&gt;();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//añadimos el que va a ser el primer elemento </span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">linkedList.AddFirst(<span style="color: #006080">&quot;Alfa&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//mantenemos una instanacia al ultimo </span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//elemento insertado mediante linkedListNode</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//NOTA: tambien podriamos utilizar linkedList.AddLast...</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var linkedListNode = linkedList.AddAfter(linkedList.First, <span style="color: #006080">&quot;Beta&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">linkedListNode = linkedList.AddAfter(linkedListNode, <span style="color: #006080">&quot;Eco&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">linkedListNode = linkedList.AddAfter(linkedListNode, <span style="color: #006080">&quot;November&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//como por ejemplo aqui</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">linkedList.AddLast(<span style="color: #006080">&quot;Romeo&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">linkedList.AddLast(<span style="color: #006080">&quot;Zulu&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//insertamos despues de &quot;Eco&quot;</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">linkedListNode = linkedList.Find(<span style="color: #006080">&quot;Eco&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">if</span> (linkedListNode != <span style="color: #0000ff">null</span>) </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    linkedList.AddBefore(linkedListNode, <span style="color: #006080">&quot;Delta&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">foreach</span>(var elemento <span style="color: #0000ff">in</span> linkedList)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Console.WriteLine(elemento);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//resultados...</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//Alfa</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//Beta</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//Eco</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//November</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//Romeo</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">//Zulu</pre>
</p></div>
</div>
<h2>Colecciones específicas </h2>
<p>Otro tipo de agrupamiento para las colecciones específicas (es decir, las que se extienden más allá de <strong>ICollection</strong>) es de colecciones de acceso por clave o por índice. A continuación veremos el conjunto de colecciones agrupadas por clave (a través de <strong>IDictionary&lt;TKey, TValue&gt;</strong>) y posteriormente las que son agrupadas por índice (a través de <strong>IList&lt;T&gt;</strong>) y por último aquellas colecciones que hacen uso tanto de las características de índice y de clave.</p>
<h3>Derivadas de System.Collections.Generics.IList&lt;T&gt;</h3>
<p>En el ejemplo con el que abríamos el primer post vimos un ejemplo con <strong>System.Array</strong>, un claro ejemplo de colección basado en índices, también conocido en inglés como zero-based indexed collection. <strong>System.ArrayList </strong>es otro claro ejemplo de colección indexada que surge de la necesidad de poder redimensionar el tamaño de una colección de forma dinámica, limitación encontrada en <strong>System.Array</strong>. De hecho, <strong>ArrayList </strong>podría ser descrita como un híbrido entre una colección y una matriz de elementos ya que los elemento siguen almacenándose en orden de llegada pese a que pueden ser obtenidos mediante el índice relativo de la propia colección. </p>
<p><strong><a href="http://msdn.microsoft.com/en-us/library/6sh2ey19(v=VS.100).aspx" target="_blank">System.Collections.Generics.List&lt;T&gt;</a></strong> es equivalente natural a <strong>ArrayList </strong>en la parte de genericos. Es en esencial un <strong>ArrayList mejorada en rendimiento, tamaño y velocidad</strong>. Es quizás la colección más utilizada y más adecuada a la gran mayoria de escenarios debido a su flexibilidad y características.</p>
<p>En cuanto a la capacidad, de la misma forma que en ArrayList, podemos indicársela en el constructor o bien no hacerlo y la propia colección irá redimensionándose. Debido al coste que tiene la operación de redimensionamiento, <strong>List&lt;T&gt;</strong> crece en bloques del doble de su capacidad, siendo 4 su capacidad inicial, si no se indica el tamaño por defecto. Esto significa que tras la instanciación de:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var list = <span style="color: #0000ff">new</span> List&lt;<span style="color: #0000ff">double</span>&gt; { 20.0d };</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//list.Capacity = 4</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">//list.Count = 1</pre>
</p></div>
</div>
<p>El objeto list poseerá una capacidad de 4 elementos, tres de ellos null. Tras ir añadiendo más elementos list siempre irá creciendo en orden de 4, 8, 16, 32, 64, … elementos dejando los no utilizados con el valor por defecto de cada tipo, esto es default(T), o más concretamente null para los tipos por referencia y 0 para los tipos por valor. Indentificaremos la capacidad total mediante la propiedad Capacity, siempre multiples de 4, y la cantidad de elementos no null/0 mediante Count.</p>
<p>Podemos exigir que un objeto <strong>List&lt;T&gt; </strong>tenga un tamaño fijo predefinido. Sin embargo, es muy común confundir esta limitación o característica con la cantidad de elementos que se le puede pasar por parámetro en el constructor de la clase <strong>List&lt;T&gt;</strong>. Otro error común es indicar una capacidad elevada en previsión de crecimiento. Si es cierto que esto es una buena practica si el numero de elementos a añadir es finalmente inferior a la capacidad inicial, no lo es si el numero de elementos final es superior puesto que el crecimiento de la ArrayList será multiple a la capacidad inicial. Es decir, por ejemplo, el siguiente ejemplo crea un <strong>List&lt;T&gt; </strong>con un tamaño predefinido de 100 elementos. Por un lado podemos ver como el objeto arrayList acepta 1000 elemento (10 veces más de lo inicialmente previsto) sin ningún problema. Sin embargo, tras el bucle podemos observar que la capcidad –Capacity- (no cantidad –Count- de elementos) es de 1600 y es debido a que el crecimiento ha sido de 100, 200, 400, 800 y 1600. O sea que tenemos un <strong>List&lt;T&gt;</strong> de 1600 elementos de capacidad y realmente se estan utilizando 1000.</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var list = <span style="color: #0000ff">new</span> List&lt;<span style="color: #0000ff">string</span>&gt;(100);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt;= 1000; i++)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    list.Add(i.ToString());</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//list.Capacity = 1600</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">//list.Count = 1000</pre>
</p></div>
</div>
<p>Si hacemos la misma interación sin un tamaño predefinido, la capacidad final será de 1024.</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var list = <span style="color: #0000ff">new</span> List&lt;<span style="color: #0000ff">string</span>&gt;();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt;= 1000; i++)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    list.Add(i.ToString());</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//list.Capacity = 1024</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">//list.Count = 1000</pre>
</p></div>
</div>
<h3>Derivadas de System.Collections.Generics.IDictionary&lt;T&gt;</h3>
<p>La diferencia fundamental entre <strong><a href="http://msdn.microsoft.com/en-us/library/dd321008(v=VS.100).aspx" target="_blank">SortedList&lt;TKey,TValue&gt;</a> </strong>y <strong>List&lt;T&gt;</strong> es que mientras en <strong>List&lt;T&gt; </strong>se almacenan los elemento por orden de “llegada” en <strong>SortedList&lt;TKey,TValue&gt;</strong> se almacena una pareja valor/clave y tanto se puede acceder por clave como por índice.</p>
<p>Internamente, <strong>SortedList&lt;TKey,TValue&gt; </strong>mantiene dos objetos <strong>IList&lt;T&gt;</strong> internos, uno para el almacenamiento de claves y otro para el almacenamiento de valores la cuales se acceden mediante un objeto de la clase <strong><a href="http://msdn.microsoft.com/en-us/library/5tbh8a42(v=VS.100).aspx" target="_blank">KeyValuePair&lt;TKey,TValue&gt;</a></strong>. Como su propio nombre indica, <strong>SortedList&lt;TKey,TValue&gt; </strong>mantiene el conjunto de elementos ordenados por clave. La clave puede ordenarse en función a una implementación específica de <strong>IComparer&lt;T&gt; </strong>o bien mediante la interfaz <strong>IComparable&lt;T&gt; </strong>que los propios tipos de las claves – de sus elementos &#8211; indican. En el siguiente ejemplo añadimos 4 elementos de tipo string y observamos el resultado. Fijémonos como el IComparer&lt;T&gt; por defecto es CaseSensitive es decir System.Collections.Generic.Comparer&lt;T&gt;.Default (ya que está utilizando la implementación <strong>IComparable&lt;T&gt; </strong>derivado de la clase String).</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var sortedList = <span style="color: #0000ff">new</span> SortedList&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">string</span>&gt;()</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                 {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                     {<span style="color: #006080">&quot;Z&quot;</span>, <span style="color: #006080">&quot;Z&quot;</span>},</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                     {<span style="color: #006080">&quot;A&quot;</span>, <span style="color: #006080">&quot;A&quot;</span>},</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                     {<span style="color: #006080">&quot;a&quot;</span>, <span style="color: #006080">&quot;a&quot;</span>},</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                     {<span style="color: #006080">&quot;B&quot;</span>, <span style="color: #006080">&quot;B&quot;</span>}</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                 };</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">foreach</span> (var elemento <span style="color: #0000ff">in</span> sortedList)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Console.WriteLine(elemento.Value);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//Resultado:</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//a</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//A</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//B</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">//Z</pre>
</p></div>
</div>
<p>Sin embarago, si utilizáramos un <strong>IComparer&lt;T&gt;</strong> propio y especificaramos explicitamente que la comparación sea Case Insensitive, obtendriamos un error ya que no es posible añadir dos elemento con la misma clave al tratar de la misma forma o valor las cadenas “a” y “A”:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> CaseInsensitiveComparerSample&lt;T&gt; : IComparer&lt;T&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Compare(T x, T y)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">return</span> ((<span style="color: #0000ff">new</span> CaseInsensitiveComparer())</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            .Compare(y, x));</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #008000">//implementación</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var sortedList = <span style="color: #0000ff">new</span> SortedList&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">string</span>&gt;(</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">new</span> CaseInsensitiveComparerSample&lt;<span style="color: #0000ff">string</span>&gt;()</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                )</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                 {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                     {<span style="color: #006080">&quot;Z&quot;</span>, <span style="color: #006080">&quot;Z&quot;</span>},</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                     {<span style="color: #006080">&quot;A&quot;</span>, <span style="color: #006080">&quot;A&quot;</span>},</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                     <span style="color: #008000">// Argument Exception</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                     {<span style="color: #006080">&quot;a&quot;</span>, <span style="color: #006080">&quot;a&quot;</span>},<span style="color: #008000">// Item has already been added. </span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                     <span style="color: #008000">// Key in dictionary: 'A'  Key being added: 'a'</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                     {<span style="color: #006080">&quot;B&quot;</span>, <span style="color: #006080">&quot;B&quot;</span>}</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                 };</pre>
</p></div>
</div>
<h4><a href="http://msdn.microsoft.com/en-us/library/xfhwa508(v=VS.100).aspx" target="_blank">Dictionary&lt;TKey,TValue&gt;</a></h4>
<p><strong>Dictionary&lt;TKey, TValue&gt;</strong> es un <strong>HashTable</strong> fuertemente tipado. La diferencia entre ambos se encuentra en como gestiona las colisiones de los codigos hash de sus elementos en el algoritmo hash. Mientras que HasTable realiza un sondeo cuando existe una colision reubicando el elemento en el siguiente cubo –bucket- libre mientras que <strong>Dictionary&lt;TKey, TValue&gt;</strong> realiza un encadenamiento de los elementos con un mismo codigo hash almacenándolos en una lista asociada a un mismo cubo –<strong>bucket</strong>-. Para obtener más información al respecto podeis consultar el post <a href="http://blogs.msdn.com/b/kcwalina/archive/2004/08/06/210297.aspx" target="_blank">System.Collections.Generic Dictionary Capacity, Hashing, and Collisions</a>.</p>
<p>Por lo demás y como comentamos antes el funcionamiento es en esencial el de un <strong>HashTable</strong>. Todos los elementos implementan de forma explícita o implícita un código Hash, a través, por ejemplo, de la clase base <strong>System.Object. </strong>Como peculiaridad, <strong>Dictionary&lt;TKey,TValue&gt;</strong> utiliza la también estructura <strong><a href="http://msdn.microsoft.com/en-us/library/5tbh8a42.aspx" target="_blank">KeyValuePair&lt;TKey,TValue&gt;</a> </strong>en lugar de la estructura <strong>DictionaryEntry</strong> que utiliza <strong>HasTable</strong>. Un ejemplo sencillo seria:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var dictionary = <span style="color: #0000ff">new</span> Dictionary&lt;<span style="color: #0000ff">int</span>,<span style="color: #0000ff">string</span>&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                    {616000, <span style="color: #006080">&quot;Juan M.&quot;</span>},</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                    {616100, <span style="color: #006080">&quot;Rafael H.&quot;</span>},</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                    {616200, <span style="color: #006080">&quot;Joaquin A.&quot;</span>},</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                    {616210, <span style="color: #006080">&quot;Alberto P.&quot;</span>}</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                                };</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">foreach</span> (KeyValuePair&lt;<span style="color: #0000ff">int</span>,<span style="color: #0000ff">string</span>&gt; elemento <span style="color: #0000ff">in</span> dictionary)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Console.WriteLine(</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">string</span>.Format(<span style="color: #006080">&quot;Key: {0} Value:{1}&quot;</span>,</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                        elemento.Key, elemento.Value));</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
</p></div>
</div>
<h4><a href="http://msdn.microsoft.com/en-us/library/f7fta44c(v=VS.100).aspx" target="_blank">SortedDictionary&lt;TKey,TValue&gt;</a> </h4>
<p>La colección que mezcla las capacidades del <strong>Dictonary&lt;TKey, TValue&gt; </strong>con un conjunto de claves ordenado como hacía <strong>SortedList&lt;T&gt;</strong> es <strong>SortedDictionary&lt;TKey, TValue&gt;</strong>. Aqui es dónde reside la mayoría de las preguntas que nos hacemos acerca de cuál tipo de colección utilizar. </p>
<p>Según información del MSDN Library, establece que:</p>
<p><em>Where the two classes differ is in memory use and speed of insertion and removal:</em></p>
<ul>
<li>
<p><a href="http://msdn.microsoft.com/en-us/library/ms132319.aspx"><em>SortedList&lt;TKey, TValue&gt;</em></a><em> uses less memory than SortedDictionary&lt;TKey, TValue&gt;.</em></p>
</li>
<li>
<p><em>SortedDictionary&lt;TKey, TValue&gt; has faster insertion and removal operations for unsorted data: O(log n) as opposed to O(n) for </em><a href="http://msdn.microsoft.com/en-us/library/ms132319.aspx"><em>SortedList&lt;TKey, TValue&gt;</em></a><em>.</em></p>
</li>
<li>
<p><em>If the list is populated all at once from sorted data, </em><a href="http://msdn.microsoft.com/en-us/library/ms132319.aspx"><em>SortedList&lt;TKey, TValue&gt;</em></a><em> is faster than SortedDictionary&lt;TKey, TValue&gt;.</em></p>
</li>
</ul>
<p>Veamos las diferencias reales de los dos primeros puntos: memoria y rendimiento.</p>
<h5>Memoria</h5>
<p>Para tratar de hacer una comparativa del tamaño de memoria utilizado entre uno y otro vamos a ejecutar un código que lo único que hará es añadir 1.000.000 de elementos string de clave int. </p>
<blockquote>
<p>NOTA: La inserción se hace de forma secuencias con lo que el coste en la operación de agregación será mucho menor en <strong>SortedList</strong>. Esto es una excepción puesto que por norma general el valor de la clave será desordenada y es en ese punto, cuando hablemos del rendimiento lo veremos, dónde <strong>SortedDictionary </strong>es mucho más rápido.</p>
</blockquote>
<p>Lo realmente importante del siguiente código es el espacio en memoria que se utiliza para la manipulación de los dos objectos <strong>SortedList </strong>y <strong>SortedDictionary</strong>:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var sortedList =</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">new</span> SortedList&lt;<span style="color: #0000ff">int</span>, <span style="color: #0000ff">string</span>&gt;();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var sortedDictionary =</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">new</span> SortedDictionary&lt;<span style="color: #0000ff">int</span>, <span style="color: #0000ff">string</span>&gt;();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">int</span> items = 1000000;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; items; i++)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        sortedList.Add(i,i.ToString());</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; items; i++)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    sortedDictionary.Add(i,i.ToString());</pre>
</p></div>
</div>
<p>Tras la ejecución con <strong>CLRProfiler </strong>vemos el grafo de asignación de bytes en memoria de:</p>
<p>la SortedList:</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/06/image16.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/06/image16_thumb.png" width="708" height="130" /></a> </p>
<p>y la representacción de SortedDictionary:</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/06/image12.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/06/image12_thumb.png" width="702" height="158" /></a> </p>
<p>Como podemos comprobar el espacio asignado en memoria es superior en el caso de SortedDictionary.</p>
<p>Como curiosidad podemos apreciar como cada uno de los objetos trata de forma bien distinta la información en memoria. <strong>SortedList </strong>al final acaba generando dos <strong>Arrays</strong>, una de claves y otra de valores del tipo <strong>System.Int32[] </strong>y <strong>System.String[] </strong>respectivamente. Por otro lado, <strong>SortedDictionary </strong>hace uso de un tipo utilizado internamente llamado <strong>Generic.TreeSet</strong>. Este tipo es un arbol binario utilizado, como dije antes, <strong>de forma interna </strong>para la manipulación de objetos del tipo <strong>Dictionary</strong>. <strong>La razón por la cual no está expuesta como API pública </strong>es debido a que el enfoque de este mismo tipo seria muy diferente. Básicamente Microsoft decidió utilizar únicamente de forma interna y no exponerla como API pública. Además de <strong>Generic.TreeSet </strong>podemos ver un objeto <strong>System.String </strong>que es dónde almacena el valor del objeto <strong>SortedDictionary</strong>.</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">internal</span> <span style="color: #0000ff">class</span> TreeSet&lt;T&gt; : ICollection&lt;T&gt;, IEnumerable&lt;T&gt;, ICollection, </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    IEnumerable, ISerializable, IDeserializationCallback </pre>
</p></div>
</div>
<h5>Rendimiento</h5>
<p>Para trata de evaluar el rendimiento entre ambas colecciones vamos a realizar una prueba muy similar, pero no vamos a dar ventaja a SortedList añadiendo elementos ordenados y es por ello que vamos a substituir las claves del tipo int al tipo <strong>System.Guid </strong>generándose uno diferente cada vez de forma que <strong>SortedList </strong>tenga que ordenar la clave tras cada inserción.</p>
<p>El código podria ser algo tal que así:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var sortedList =</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">new</span> SortedList&lt;Guid, <span style="color: #0000ff">string</span>&gt;();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var sortedDictionary =</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">new</span> SortedDictionary&lt;Guid, <span style="color: #0000ff">string</span>&gt;();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">int</span> items = 100000;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; items; i++)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        sortedList.Add(Guid.NewGuid(), i.ToString());</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; items; i++)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    sortedDictionary.Add(Guid.NewGuid(), i.ToString());</pre>
</p></div>
</div>
<p>El resultado del <strong>Performance Profiling del VS 2010 </strong>es tal y como muestra la siguiente captura de pantalla.</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/06/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/06/image_thumb.png" width="939" height="187" /></a> </p>
<p>La operación <strong>SortedList.Add </strong>ocupa más del 97% del tiempo total de ejecución del código mientras que <strong>SortedDictionary.Add </strong>apenas llega al 2% por lo tanto queda demostrado el rendimiento entre ambas clases en operaciones de inserción y eliminación.</p>
<h1>Conclusión</h1>
<p>Además de las nombradas en este post existen otras menos utilizadas dentro del espacio de nombre de colecciones genéricas. </p>
<h2>Otras referencias</h2>
<p><a href="http://en.how-to.mobi/index.php?sd=es&amp;id=221691">http://en.how-to.mobi/index.php?sd=es&amp;id=221691</a></p>
<p>&#160;<a href="http://blogs.msdn.com/b/kcwalina/archive/2004/08/06/210297.aspx">http://blogs.msdn.com/b/kcwalina/archive/2004/08/06/210297.aspx</a></p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://josemigueltorres.net/boresight/?p=411&amp;feed=comments-rss2" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://josemigueltorres.net/boresight/?p=411&amp;title=-Colecciones+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28II%29" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://josemigueltorres.net/boresight/?p=411&amp;title=-Colecciones+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28II%29" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://josemigueltorres.net/boresight/?p=411&amp;title=-Colecciones+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28II%29&amp;desc=%20%20Introducci%C3%B3n%20%20Esta%20serie%20de%20art%C3%ADculos%20pretende%20mostrar%20las%20caracter%C3%ADsticas%20y%20peculiaridades%20de%20las%20colecciones%20en%20.NET.%20El%20objetivo%20no%20es%20mostrar%20las%20situaciones%20en%20las%20que%20cada%20una%20de%20los%20diferentes%20tipos%20de%20colecciones%20pueden%20utilizarse%20en%20cada%20contexto%20sino%20la%20de%20conocer%20las%20mismas%2C%20ventajas" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://josemigueltorres.net/boresight/?p=411&amp;t=-Colecciones+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28II%29" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://josemigueltorres.net/boresight/?p=411&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://josemigueltorres.net/boresight/?p=411&amp;title=-Colecciones+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28II%29&amp;summary=%20%20Introducci%C3%B3n%20%20Esta%20serie%20de%20art%C3%ADculos%20pretende%20mostrar%20las%20caracter%C3%ADsticas%20y%20peculiaridades%20de%20las%20colecciones%20en%20.NET.%20El%20objetivo%20no%20es%20mostrar%20las%20situaciones%20en%20las%20que%20cada%20una%20de%20los%20diferentes%20tipos%20de%20colecciones%20pueden%20utilizarse%20en%20cada%20contexto%20sino%20la%20de%20conocer%20las%20mismas%2C%20ventajas&amp;source=josemiguel.torres" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://josemigueltorres.net/boresight/?p=411&amp;bm_description=-Colecciones+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28II%29&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://josemigueltorres.net/boresight/?p=411&amp;title=-Colecciones+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28II%29" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://josemigueltorres.net/boresight/?p=411&amp;title=-Colecciones+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28II%29" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://josemigueltorres.net/boresight/?p=411&amp;title=-Colecciones+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28II%29" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://josemigueltorres.net/boresight/?p=411" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=-Colecciones+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28II%29+-+http://b2l.me/3x6xm&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-yahoomail">
			<a href="http://compose.mail.yahoo.com/?Subject=-Colecciones+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28II%29&amp;body=Link: http://josemigueltorres.net/boresight/?p=411 (sent via shareaholic)%0D%0A%0D%0A----%0D%0A %20%20Introducci%C3%B3n%20%20Esta%20serie%20de%20art%C3%ADculos%20pretende%20mostrar%20las%20caracter%C3%ADsticas%20y%20peculiaridades%20de%20las%20colecciones%20en%20.NET.%20El%20objetivo%20no%20es%20mostrar%20las%20situaciones%20en%20las%20que%20cada%20una%20de%20los%20diferentes%20tipos%20de%20colecciones%20pueden%20utilizarse%20en%20cada%20contexto%20sino%20la%20de%20conocer%20las%20mismas%2C%20ventajas" rel="nofollow" class="external" title="Email this via Yahoo! Mail">Email this via Yahoo! Mail</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://josemigueltorres.net/boresight/?feed=rss2&amp;p=411</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://josemigueltorres.net/boresight/?p=411</feedburner:origLink></item>
		<item>
		<title>-Colecciones No Genericas- Coleccionando objetos en .NET Framework (I)</title>
		<link>http://feedproxy.google.com/~r/josemigueltorres/rXsu/~3/WcGbFzzi12M/</link>
		<comments>http://josemigueltorres.net/boresight/?p=404#comments</comments>
		<pubDate>Tue, 08 Jun 2010 17:54:13 +0000</pubDate>
		<dc:creator>josemiguel.torres</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[System.Collections]]></category>
		<category><![CDATA[Visual C#]]></category>

		<guid isPermaLink="false">http://josemigueltorres.net/boresight/?p=404</guid>
		<description><![CDATA[Introducción Esta serie de artículos pretende mostrar las características y peculiaridades de las colecciones en .NET. El objetivo no es mostrar las situaciones en las que cada una de los diferentes tipos de colecciones pueden utilizarse en cada contexto sino la de conocer las mismas, ventajas y desventajas, para poder seleccionar el tipo de colección [...]]]></description>
			<content:encoded><![CDATA[<h1>Introducción</h1>
<p>Esta serie de artículos pretende mostrar las características y peculiaridades de las colecciones en .NET. El objetivo no es mostrar las situaciones en las que cada una de los diferentes tipos de colecciones pueden utilizarse en cada contexto sino la de conocer las mismas, ventajas y desventajas, para poder seleccionar el tipo de colección más apropiado en cada momento.</p>
<p>La serie cubre (entre corchetes el estado):</p>
<ul>
<li><strong>Colecciones No Genericas (actual post)</strong></li>
<li><a href="http://josemigueltorres.net/boresight/?p=411">Colecciones Genéricas de System.Collections.Generic</a></li>
<li>Colecciones de System.Collections.ObjectModel (en mi cabeza)</li>
<li>Colecciones nuevas en CLR 4.0 (no es definitivo además solo hay una)</li>
<li>Cota superior asintótica de las colecciones del CLI. aka Notación de Landau &#8211; O Grande- (en desarrollo)</li>
<li>Power Collections y C5 (por ahí andan)</li>
<li>Rx  (si hay ganas y tiempo)</li>
</ul>
<h1>¿Que es una colección?</h1>
<p>Se entiende como colección un conjunto de entidades lógicamente relacionadas –es decir que deberían tener algo en común-. Así una colección de objetos de tipo string podría ser declarada como un System.Array de la siguiente forma en C#.</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var cadenas = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">string</span>[]</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                     {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                        <span style="color: #006080;">"A"</span>, <span style="color: #006080;">"B"</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                     };</pre>
</div>
</div>
<p>Sin embargo, esta declaración tiene algunos matices como por ejemplo que en muy pocas ocasiones conocemos el tamaño fijo que tendrá y por lo tanto se convierte en una limitación del propio System.Array, por ejemplo. Básicamente, las características y comportamiento del tipo de colección a utilizar nos ayudará o limitará en mayor o menor medida.</p>
<p>Por lo tanto, primero de todo me gustaría empezar definiendo que es para <strong>.NET Framework</strong> una colección. Desde el punto de vista de .NET Framework, una colección es cualquier tipo que implemente <strong><a href="http://msdn.microsoft.com/es-es/library/system.collections.icollection(v=VS.100).aspx" target="_blank">System.Collections.ICollection</a></strong>, <strong><a href="http://msdn.microsoft.com/es-es/library/system.collections.idictionary(v=VS.100).aspx" target="_blank">System.Collections.IDictionary</a></strong> o <strong><a href="http://msdn.microsoft.com/es-es/library/system.collections.ilist(v=VS.100).aspx" target="_blank">System.Collections.IList</a></strong>, sin embargo no siempre tenemos claro que otorga especialemente cada una de estas interfaces. Vayamos por partes.</p>
<h2>El Enumerador</h2>
<p>Si empezaramos a desgranar todos los tipos de colecciones disponibles en el <strong>CLR</strong> encontraríamos un denominador común “oculto”, la interfaz <strong><a href="http://msdn.microsoft.com/es-es/library/system.collections.ienumerator(v=VS.100).aspx" target="_blank">IEnumerator</a></strong>. Si para que una colección sea considerada como tal en .NET Fx debe implementar una de las 3 interfazes expuestas anteriormente, todas estas interfaces deben implementar <strong><a href="http://msdn.microsoft.com/es-es/library/system.collections.ienumerable(v=VS.100).aspx" target="_blank">IEnumerable</a></strong> la cual no hace más exponer un <strong>IEnumerator</strong>.</p>
<p>Podríamos empezar mostrando la firma tanto de <strong>ICollection</strong>:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">[ComVisible(<span style="color: #0000ff;">true</span>)]</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span> ICollection : IEnumerable</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">int</span> Count { get; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">object</span> SyncRoot { get; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">bool</span> IsSynchronized { get; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">void</span> CopyTo(Array array, <span style="color: #0000ff;">int</span> index);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
</div>
</div>
<p>como de <strong>IDictionary</strong></p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">[ComVisible(<span style="color: #0000ff;">true</span>)]</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span> IDictionary : ICollection, IEnumerable</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">object</span> <span style="color: #0000ff;">this</span>[<span style="color: #0000ff;">object</span> key] { get; set; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    ICollection Keys { get; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    ICollection Values { get; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">bool</span> IsReadOnly { get; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">bool</span> IsFixedSize { get; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">bool</span> Contains(<span style="color: #0000ff;">object</span> key);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">void</span> Add(<span style="color: #0000ff;">object</span> key, <span style="color: #0000ff;">object</span> <span style="color: #0000ff;">value</span>);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">void</span> Clear();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">new</span> IDictionaryEnumerator GetEnumerator();</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">void</span> Remove(<span style="color: #0000ff;">object</span> key);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
</div>
</div>
<p>como de <strong>IList</strong>.</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">[ComVisible(<span style="color: #0000ff;">true</span>)]</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span> IList : ICollection, IEnumerable</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">object</span> <span style="color: #0000ff;">this</span>[<span style="color: #0000ff;">int</span> index] { get; set; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">bool</span> IsReadOnly { get; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">bool</span> IsFixedSize { get; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">int</span> Add(<span style="color: #0000ff;">object</span> <span style="color: #0000ff;">value</span>);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">bool</span> Contains(<span style="color: #0000ff;">object</span> <span style="color: #0000ff;">value</span>);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">void</span> Clear();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">int</span> IndexOf(<span style="color: #0000ff;">object</span> <span style="color: #0000ff;">value</span>);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">void</span> Insert(<span style="color: #0000ff;">int</span> index, <span style="color: #0000ff;">object</span> <span style="color: #0000ff;">value</span>);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">void</span> Remove(<span style="color: #0000ff;">object</span> <span style="color: #0000ff;">value</span>);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">void</span> RemoveAt(<span style="color: #0000ff;">int</span> index);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
</div>
</div>
<p>Y para empezar nos encontramos con otra característica que seguro muchos no habíamos visto anteriormente y es que <strong>tanto IDictionary com IList implementan, a su vez ICollection</strong>.</p>
<p>Es aquí, en este punto, dónde empieza a avistarse la diferencia entre las tres interfaces y podemos catalogarlas en dos conjuntos: <strong>Colecciones estándar o regulares</strong> y <strong>Colecciones específicas</strong>. Es obvio que tras ver la firma de cada una de ellas podemos afirmar que <strong>IList</strong> y <strong>IDictionary</strong> son colecciones específicas (ambas extienden ICollection) e <strong>ICollection</strong> es una colección estándard o regular.</p>
<blockquote><p><em>NOTA: Utilizo el térmio “específicas” no especializadas –Specialized- cuyas colecciones estan en el espacio <strong>System.Collections.Specialized</strong> y que veremos más tarde.</em></p></blockquote>
<p>Primera pregunta que surge, ¿Conociendo que <strong>System.Collection.List</strong> es el principal “consumidor” de <strong>IList</strong> y que <strong>Collection</strong> hace lo mismo de <strong>ICollection</strong>, cuál de ellas deberíamos utilizar? La respuesta es relativamente sencilla. Collection, a diferencia de List es una clase abstracta y por tanto debe ser implementada para ser extendida. Esto nos da una pista que la solución reside en que Collection debe ser utilizada para ser expuesta a través de un API pública mientras que List implementa métodos de búsqueda y ordenación específicas listas para usar.</p>
<p>Volviendo al núcleo central del tema, es decir a <strong>IEnumerator</strong>, observamos que las tres interfaces además implementan <strong>IEnumerable</strong> con lo que es buen momento par aver la firma de la misma:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span> IEnumerable</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    IEnumerator GetEnumerator();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
</div>
</div>
<p>Tal y como se puede observar, y se ha comentado anteriormente, <strong>IEnumerable</strong> no hace más que retornar un <strong>IEnumerator</strong>. Es importante conocer y entender la figura de un enumerador/iterador pues es el que va a permitir la iteración sobre cada uno de los elementos de una colección. La sentencia <strong><em>foreach</em></strong>, por ejemplo, hace uso de él. <strong>IEnumerator</strong> únicamente permite la iteración, nunca la modificación del elemento. De hecho, si observamos su firma:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span> IEnumerator</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">object</span> Current { get; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">bool</span> MoveNext();</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">void</span> Reset();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
</div>
</div>
<p>Podemos ver que, exluyendo <strong>Reset</strong>, tanto <strong>Current</strong> como <strong>MoveNext</strong> son métodos de navegación o iteración entre collecciones. El mecanismo es sencillo; <strong>Current</strong> indica un valor indefinido y por tanto se lanza una llamada a <strong>MoveNext</strong>. Si esta retorna <strong>True</strong>, <strong>Current</strong> contiene el primer elemento de la colección y así lo hará mientras el valor de <strong>MoveNext</strong> vaya siendo <strong>True</strong>. En el momento que éste sea <strong>False</strong>, <strong>Current</strong> indicará al último elemento de la colección.</p>
<blockquote><p><em>NOTA: El método <strong>Reset</strong> se mantiene únicamente por compatibilidad COM con lo que queda fuera del alcance de este artículo.</em></p></blockquote>
<h1>Colecciones No Genéricas</h1>
<h2>Colecciones regulares (System.Collections.ICollection)</h2>
<p>Este tipo de colecciones también son conocidas como colecciones ordenadas(*) o simplemente collecciones –me he permitido la libertad de denominarlas estándar o regulares para diferenciarlas de las específicas-. Hago hincapié en ello pues encontré varias formas de denomiarlas. De hecho, más allá de la colección <strong>Collection</strong> que como vimos anteriormente <strong>es abstracta</strong> y no es más que una evolución de <strong><a href="http://msdn.microsoft.com/es-es/library/system.collections.collectionbase(v=VS.100).aspx" target="_blank">CollectionBase</a></strong> utilizada en las versiones tempranas del <strong>CLR</strong>, existe un par de clases que podriamos catalogar dentro de este grupo y que vienen representadas por las clases <strong><a href="http://msdn.microsoft.com/es-es/library/system.collections.stack(v=VS.100).aspx" target="_blank">Stack</a></strong> y <strong><a href="http://msdn.microsoft.com/es-es/library/system.collections.queue(v=VS.100).aspx" target="_blank">Queue</a></strong>.</p>
<blockquote><p><em>NOTA: (*) Ambas guardan una estrecha relación ya que en ambos casos los elementos se almacenan según el orden de llegada. Nótese que el término “ordenacióin” utilizado anteriormente no es una ordenación lógica sobre una propiedad del conjunto de elemento; este tipo de elementos los veremos más adelante y podrían estar catalogados como colecciones especializadas.</em></p></blockquote>
<p>La diferencia principal entre <strong>Stack</strong> y <strong>Queue</strong> reside en el orden en el que los elementos son obtenidos. En ambos casos almacenan elementos en estricto orden de llegada y mientras que en la clase Stack responde a <strong>LIFO</strong> (Last Input First Output) en el caso de la cola responde a <strong>FIFO</strong> (First Input First Output). Ambas clases derivan de <strong>ICollection</strong> e <strong>IClonable</strong> y cada una de ellas utiliza un método distinto para añadir y quitar un elemento de la colección. <strong>Push</strong> y <strong>Pull</strong> para Stack y <strong>Enqueue</strong> y <strong>Dequeu</strong> para Queue.</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var queue = <span style="color: #0000ff;">new</span> Queue();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var stack = <span style="color: #0000ff;">new</span> Stack();</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">queue.Enqueue(<span style="color: #006080;">"A"</span>);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">queue.Enqueue(<span style="color: #006080;">"B"</span>);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">queue.Enqueue(<span style="color: #006080;">"C"</span>);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">queue.Enqueue(<span style="color: #006080;">"D"</span>);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">stack.Push(<span style="color: #006080;">"A"</span>);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">stack.Push(<span style="color: #006080;">"B"</span>);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">stack.Push(<span style="color: #006080;">"C"</span>);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">stack.Push(<span style="color: #006080;">"D"</span>);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">Console.WriteLine(<span style="color: #006080;">"Dequeue-ing..."</span>);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">foreach</span> (var elemento <span style="color: #0000ff;">in</span> queue)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    Console.WriteLine(elemento.ToString());</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">Console.WriteLine(<span style="color: #006080;">"Pop-ing..."</span>);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">foreach</span> (var elemento <span style="color: #0000ff;">in</span> stack)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    Console.WriteLine(elemento.ToString());</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">//Resultado:</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">// Dequeue-ing...</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">// A</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">// B</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">// C</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">// D</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">// Pop-ing...</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">// D</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">// C</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">// B</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">// A</pre>
</div>
</div>
<p>Son colecciones principalmente utilizadas, como es obvio, en contextos dónde el orden de llegada es altamente importante. A partir de aquí, la forma en la que se obtienen los elementos de la colección es la clave para decidir seleccionar una u otra clase.</p>
<h2>Colecciones específicas</h2>
<p>Otro tipo de agrupamiento para las colecciones específicas (es decir, las que se extienden más allá de <strong>ICollection</strong>) es de colecciones de <strong>acceso por clave o por índice</strong>. A continuación veremos el conjunto de colecciones agrupadas por clave (a través de <strong>IDictionary</strong>) y posteriormente las que son agrupadas por índice (a través de <strong>IList</strong>) y por último aquellas colecciones que hacen uso tanto de las características de índice y de clave.</p>
<h3>Derivadas de System.Collections.IList</h3>
<p>En el ejemplo con el que abríamos este artículo vimos un ejemplo con <strong>System.Array</strong>, un claro ejemplo de colección basado en índices, también conocido en inglés como zero-based indexed collection. <strong><a href="http://msdn.microsoft.com/es-es/library/system.collections.arraylist(v=VS.100).aspx" target="_blank">System.ArrayList</a></strong> es otro claro ejemplo de colección indexada que surge de la necesidad de poder redimensionar el tamaño de una colección de forma dinámica, limitación encontrada en <strong>System.Array</strong>. De hecho, podría <strong>ArrayList </strong>ser descrita como un híbrido entre una colección y una matriz de elementos ya que los elementos siguen almacenándose en orden de llegada pese a que pueden ser obtenidos mediante el índice relativo de la propia colección.</p>
<p>En cuanto a la capacidad, podemos indicársela en el constructor o bien no hacerlo y la propia colección irá redimensionándose. Debido al <strong>coste que tiene la operación de redimensionamiento</strong>, <strong>ArrayList</strong> crece en bloques del doble de su capacidad, siendo <strong>4</strong> su capacidad inicial, si no se indica el tamaño por defecto. Esto significa que tras la instanciación de:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var arrayList2 = <span style="color: #0000ff;">new</span> ArrayList {20.0d};</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">//arrayList2.Capacity = 4</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">//arrayList2.Count = 1</pre>
</div>
</div>
<p>El objeto arrayList poseerá una capacidad de 4 elementos, tres de ellos null. Tras ir añadiendo más elementos arrayList siempre irá creciendo en orden de 4, 8, 16, 32, 64, … elementos dejando los no utilizados con null. Indentificaremos la capacidad total mediante la propiedad Capacity, siempre multiples de 4, y la cantidad de elementos no null mediante Count.</p>
<p>Podemos exigir que un objeto <strong>ArrayList</strong> tenga un tamaño fijo predefinido. Sin embargo, es muy común confundir esta limitación o característica con la cantidad de elementos que se le puede pasar por parámetro en el constructor de la clase ArrayList. Otro error común es indicar una capacidad elevada en previsión de crecimiento. Si es cierto que esto es una buena practica si el numero de elementos a añadir es finalmente inferior a la capacidad inicial, no lo es si el numero de elementos final es superior puesto que el crecimiento de la <strong>ArrayList será multiple a la capacidad inicial</strong>. Es decir, por ejemplo, el siguiente ejemplo crea un ArrayList con un tamaño predefinido de 100 elementos. Por un lado podemos ver como el objeto arrayList acepta 1000 elemento (10 veces más de lo inicialmente previsto) sin ningún problema. Sin embargo, tras el bucle podemos observar que la capcidad –Capacity- (no cantidad –Count- de elementos) es de 1600 y es debido a que el crecimiento ha sido de 100, 200, 400, 800 y 1600. O sea que <strong>tenemos un ArrayList de 1600 elementos de capacidad y realmente se estan utilizando 1000</strong>.</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var arrayList = <span style="color: #0000ff;">new</span> ArrayList(100);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; 1000; i++)</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    arrayList.Add(i);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">//arrayList.Capacity = 1600</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">//arrayList.Count = 1000</span></pre>
</div>
</div>
<p>Si hacemos la misma interación sin un tamaño predefinido –es decir con tamaño por defecto igual a 4-, la capacidad final será de 1024.</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var arrayList = <span style="color: #0000ff;">new</span> ArrayList(1);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; 1000; i++)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    arrayList.Add(i);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">//arrayList.Capacity = 1024</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">//arrayList.Count = 1000</span></pre>
</div>
</div>
<p>Otro error común es confundir tamaño predefinido o inicial con tamaño fijo. Un ArraList de tamaño fijo jamás admitirá la agregación o eliminación de elementos, pero si la modificación.</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var arrayList = <span style="color: #0000ff;">new</span> ArrayList { 20.0d };</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var arrayList2 = ArrayList.FixedSize(arrayList);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">arrayList2.Add(145.0d); <span style="color: #008000;">//NotSupportedException</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">// arrayList2.IsFixedSize = true</pre>
</div>
</div>
<p>Identificaremos las ArrayList de tamaño fijo mediante la propiedad <strong>IsFixedSize</strong> y como vemos en el ejemplo la forma de generarlas es a través de un objeto del tipo ArrayList o IList existente. Por otro lado, <strong>IsReadOnly</strong> es una propiedad de lectura y escritura con lo que podremos controlar la restricción o no de escritura sobre el conjunto de elementos.</p>
<blockquote><p><em>NOTA: ArrayList esta cayendo en desuso (si no lo ha hecho ya) desde la aparición de las colecciones genéricas.</em></p></blockquote>
<p>La colección <strong><a href="http://msdn.microsoft.com/es-es/library/system.collections.specialized.stringcollection(v=VS.100).aspx" target="_blank">StringCollection</a></strong> forma parte de las colecciones especializadas agrupadas en el espacio de nombres <a href="http://msdn.microsoft.com/es-es/library/system.collections.specialized(v=VS.100).aspx" target="_blank">System.Collections.Specialized</a>. <strong>StringCollection</strong> está especialmente optimizada para la manipulación de elementos del tipo string; es en esencia un ArraList tipado para aceptar únicamente cadenas de texto. StringCollection es ideal para la manipulación de relativamente poca cantidad de datos de tipo string que son frecuentemente actualizados.</p>
<h3>Derivadas de System.Collections.IDictionary</h3>
<p>De la misma forma que tratávamos <strong>ArrayList</strong> como “tipo primitivo” para la implementación de <strong>IList</strong>, haríamos lo mismo para el tipo <strong>SortedList</strong> con la interfaz <strong>IDictionary</strong>.La diferencia fundamental es que mientras en ArrayList se almacenan los elemento por orden de “llegada” en SortedList se almacena una pareja valor/clave y tanto se puede acceder por clave como por índice.</p>
<p>Internamente, <strong>SortedList mantiene dos arrays</strong>, uno para el almacenamiento de claves y otro para el almacenamiento de valores la cuales se acceden mediante un objeto de la estructura <strong><a href="http://msdn.microsoft.com/es-es/library/system.collections.dictionaryentry(v=VS.100).aspx" target="_blank">DictionaryEntry</a></strong>. Como su propio nombre indica, SortedList mantiene el conjunto de elementos ordenados por clave. La clave puede ordenarse en función a una implementación específica de <strong>IComparer</strong> o bien mediante la interfaz <strong>IComparable</strong> que los propios tipos de las claves – de sus elementos &#8211; indican. En el siguiente ejemplo añadimos 4 elementos de tipo string y observamos el resultado. Fijémonos como el IComparer por defecto es <strong>CaseSensitive</strong> ya que está utilizando la implementación <strong>IComparable</strong> derivado de la clase <strong>String</strong>.</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var sortedList = <span style="color: #0000ff;">new</span> SortedList()</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                     {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                         {<span style="color: #006080;">"Z"</span>, <span style="color: #006080;">"Z"</span>},</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                         {<span style="color: #006080;">"A"</span>, <span style="color: #006080;">"A"</span>},</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                         {<span style="color: #006080;">"a"</span>, <span style="color: #006080;">"a"</span>},</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                         {<span style="color: #006080;">"B"</span>, <span style="color: #006080;">"B"</span>}</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                     };</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">foreach</span> (var elemento <span style="color: #0000ff;">in</span> sortedList)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    Console.WriteLine(((DictionaryEntry)elemento).Value);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">//Resultado:</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">//a</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">//A</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">//B</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">//Z</span></pre>
</div>
</div>
<p>Sin embarago, si utilizáramos un <strong>IComparer</strong> propio y especificaramos explicitamente que la comparación sea <strong>Case Insensitive</strong>, obtendriamos un error ya que no es posible añadir dos elemento con la misma clave al tratar de la misma forma o valor las cadenas “a” y “A”:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> CaseInsensitiveComparerSample : IComparer</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Compare(Object x, Object y)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">return</span> ((<span style="color: #0000ff;">new</span> CaseInsensitiveComparer())</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            .Compare(y, x));</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000;">//implementación</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var sortedList = <span style="color: #0000ff;">new</span> SortedList(</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">new</span> CaseInsensitiveComparerSample ())</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                        {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                            {<span style="color: #006080;">"Z"</span>, <span style="color: #006080;">"Z"</span>},</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                            {<span style="color: #006080;">"A"</span>, <span style="color: #006080;">"A"</span>},</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                            <span style="color: #008000;">// Argument Exception</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                            {<span style="color: #006080;">"a"</span>, <span style="color: #006080;">"a"</span>},<span style="color: #008000;">// Item has already been added. </span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                            <span style="color: #008000;">// Key in dictionary: 'A'  Key being added: 'a'</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                            {<span style="color: #006080;">"B"</span>, <span style="color: #006080;">"B"</span>}</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                        };</pre>
</div>
</div>
<p>Otro tipo de colección basada en <strong>IDictionary</strong> es <strong><a href="http://msdn.microsoft.com/es-es/library/system.collections.hashtable(v=VS.100).aspx" target="_blank">System.Collections.Hashtable</a></strong>. <strong>Hashtable</strong> implementa tanto <strong>ICollection</strong> como <strong>IDictionary</strong> y ha sido ampliamente utilizado como tipo de colección idónea para la gestión de grandes cantidades de elementos debido a su alto rendimiento. Exige que la clave de sus elementos aunque no el valor hash de cada uno de ellos. De hecho, la ordenación del conjunto de elemento vendrá dado por <strong>el valor hash</strong>. <strong>DictionaryEntry</strong> seguirá jugando el mismo papel que lo hacía con SortedList ya que en ambos casos se almacena un par clave/valor.</p>
<p>Hablando más específicamente cada objecto de la colección Hashtable obtiene el valor hash de la llamada al método <strong>GetHashCode</strong>() heredado de <strong>System.Object</strong> o bien una propia implementación a través de la interfaz <strong>IHashCodeProvider</strong>. Cuando un elemento es agregado éste se almacenará en un sector de almacenamiento o cubo –bucket en inglés- de forma que su posterior localización será mucho más óptima. No importa el valor real del objeto pues cada uno de ellos tendrá su propio codigo hash y de este modo se reduce considerablemente el número de comparaciones. Así, por ejemplo, <strong>Hasthable</strong> situará dos elementos de tipo string con valores “<em>picnic</em>” y “<em>basket</em>” en distintos cubos o búckets; sin embargo los dos elemento de tipo string con valores “<em>stressed</em>” y “<em>desserts</em>” probablemente tengan el mismo codigo hash y por tanto los situará en un mismo bucket o cubo.</p>
<p>Realmente, la funcionalidad de un Hashtable es muy similiar al que ofrecen otras colecciones derivadas de IDictionary pero la comprensión de cómo se gestiona internamente el almacenamiento a través de códigos hash nos permite entender por un lado porque Hashtable es la colección que mayor rendimiento otorga y por otro lado las problemáticas que pueden generar la coincidencia de códigos hash si el algoritmo utilizado no es óptimo.</p>
<p>Un típico ejemplo seria:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var hashtable = <span style="color: #0000ff;">new</span> Hashtable</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                    {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                        {<span style="color: #006080;">"616-000-000"</span>, <span style="color: #006080;">"Juan M."</span>},</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                        {<span style="color: #006080;">"616-000-100"</span>, <span style="color: #006080;">"Rafael H."</span>},</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                        {<span style="color: #006080;">"616-200-000"</span>, <span style="color: #006080;">"Joaquin A."</span>},</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                        {<span style="color: #006080;">"616-200-100"</span>, <span style="color: #006080;">"Alberto P."</span>}</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                    };</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">foreach</span> (DictionaryEntry elemento <span style="color: #0000ff;">in</span> hashtable)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    Console.WriteLine(</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">string</span>.Format(<span style="color: #006080;">"Key: {0} Value:{1}"</span>,</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                        elemento.Key, elemento.Value));</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
</div>
</div>
<blockquote><p><em>NOTA: Hablaré más extensamente sobre el tema de los conflictos en colecciones Hashtable en el siguiente post cuando trate la colección Dictionary&lt;TKey, TValue&gt;</em></p></blockquote>
<p><strong><a href="http://msdn.microsoft.com/es-es/library/system.collections.specialized.stringdictionary(v=VS.100).aspx" target="_blank">StringDictionary</a></strong>, por su parte, juega el mismo papel que lo hacía <strong>StringCollection</strong>, pero si éste último era ideal para relativamente poca cantidad de elementos y su acceso es por índice, <strong>StringDictionary</strong>, al implementar <strong>IDictionary</strong> es ideal para una mayor cantidad de elementos. En este caso, también, se trata de una colección de elementos tipados ya que únicamente se aceptan elementos de tipo String.</p>
<h2>Conclusión</h2>
<p>Unicamente he expuesto las colecciones más utilizadas relativas a las no genéricas que he creído conveniente por propia experiencia exponer. En el siguiente post hablaremos de las colecciones genéricas que presenta el BCL.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://josemigueltorres.net/boresight/?p=404&amp;feed=comments-rss2" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://josemigueltorres.net/boresight/?p=404&amp;title=-Colecciones+No+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28I%29" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://josemigueltorres.net/boresight/?p=404&amp;title=-Colecciones+No+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28I%29" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://josemigueltorres.net/boresight/?p=404&amp;title=-Colecciones+No+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28I%29&amp;desc=Introducci%C3%B3n%0D%0AEsta%20serie%20de%20art%C3%ADculos%20pretende%20mostrar%20las%20caracter%C3%ADsticas%20y%20peculiaridades%20de%20las%20colecciones%20en%20.NET.%20El%20objetivo%20no%20es%20mostrar%20las%20situaciones%20en%20las%20que%20cada%20una%20de%20los%20diferentes%20tipos%20de%20colecciones%20pueden%20utilizarse%20en%20cada%20contexto%20sino%20la%20de%20conocer%20las%20mismas%2C%20ventajas%20y" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://josemigueltorres.net/boresight/?p=404&amp;t=-Colecciones+No+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28I%29" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://josemigueltorres.net/boresight/?p=404&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://josemigueltorres.net/boresight/?p=404&amp;title=-Colecciones+No+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28I%29&amp;summary=Introducci%C3%B3n%0D%0AEsta%20serie%20de%20art%C3%ADculos%20pretende%20mostrar%20las%20caracter%C3%ADsticas%20y%20peculiaridades%20de%20las%20colecciones%20en%20.NET.%20El%20objetivo%20no%20es%20mostrar%20las%20situaciones%20en%20las%20que%20cada%20una%20de%20los%20diferentes%20tipos%20de%20colecciones%20pueden%20utilizarse%20en%20cada%20contexto%20sino%20la%20de%20conocer%20las%20mismas%2C%20ventajas%20y&amp;source=josemiguel.torres" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://josemigueltorres.net/boresight/?p=404&amp;bm_description=-Colecciones+No+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28I%29&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://josemigueltorres.net/boresight/?p=404&amp;title=-Colecciones+No+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28I%29" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://josemigueltorres.net/boresight/?p=404&amp;title=-Colecciones+No+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28I%29" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://josemigueltorres.net/boresight/?p=404&amp;title=-Colecciones+No+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28I%29" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://josemigueltorres.net/boresight/?p=404" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=-Colecciones+No+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28I%29+-+http://b2l.me/z7pfu&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-yahoomail">
			<a href="http://compose.mail.yahoo.com/?Subject=-Colecciones+No+Genericas-+Coleccionando+objetos+en+.NET+Framework+%28I%29&amp;body=Link: http://josemigueltorres.net/boresight/?p=404 (sent via shareaholic)%0D%0A%0D%0A----%0D%0A Introducci%C3%B3n%0D%0AEsta%20serie%20de%20art%C3%ADculos%20pretende%20mostrar%20las%20caracter%C3%ADsticas%20y%20peculiaridades%20de%20las%20colecciones%20en%20.NET.%20El%20objetivo%20no%20es%20mostrar%20las%20situaciones%20en%20las%20que%20cada%20una%20de%20los%20diferentes%20tipos%20de%20colecciones%20pueden%20utilizarse%20en%20cada%20contexto%20sino%20la%20de%20conocer%20las%20mismas%2C%20ventajas%20y" rel="nofollow" class="external" title="Email this via Yahoo! Mail">Email this via Yahoo! Mail</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://josemigueltorres.net/boresight/?feed=rss2&amp;p=404</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://josemigueltorres.net/boresight/?p=404</feedburner:origLink></item>
		<item>
		<title>¡¡Microsoft SQL Server Compact no ha muerto!!</title>
		<link>http://feedproxy.google.com/~r/josemigueltorres/rXsu/~3/Up7wItLhk14/</link>
		<comments>http://josemigueltorres.net/boresight/?p=402#comments</comments>
		<pubDate>Wed, 14 Apr 2010 11:49:50 +0000</pubDate>
		<dc:creator>josemiguel.torres</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[.NET Compact Framework 3.5]]></category>
		<category><![CDATA[SQL Server Compact 3.5]]></category>
		<category><![CDATA[Visual Studio .NET 2010]]></category>
		<category><![CDATA[Windows Mobile 6.5]]></category>

		<guid isPermaLink="false">http://josemigueltorres.net/boresight/?p=402</guid>
		<description><![CDATA[Después del impacto que ha tenido la presentación del nuevo modelo de desarrollo que traerá tras de sí la nueva apuesta de Microsoft para telefónos móviles con Windows Phone 7 Series, y después de que más de uno se haya enfundado el traje de negro y preparado ya los preparativos para el funeral de .NET [...]]]></description>
			<content:encoded><![CDATA[<p><img style="display: inline; margin-left: 0px; margin-right: 0px;" src="http://itprocommunityguatemala.files.wordpress.com/2009/04/rosa_luto.jpg" alt="" width="219" height="164" align="left" />Después del impacto que ha tenido la presentación del nuevo modelo de desarrollo que traerá tras de sí la nueva apuesta de Microsoft para telefónos móviles con <a href="http://www.windowsphone7series.com/" target="_blank">Windows Phone 7 Series</a><strong></strong>, y después de que más de uno se haya enfundado el traje de negro y preparado ya los preparativos para <strong>el funeral de .NET Compact Framework </strong>he oído rumores de algunos desarrolladores que, ante la visible <strong>desinformación de Microsoft </strong>que rodea la aparición <strong>WP7S</strong>, han decidido incluir dentro del mismo funeral a <strong>Microsoft SQL Server Compact</strong>, y eso me duele <img src='http://jtorresd.brinkster.net/boresight/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>En primer lugar <strong><span style="font-size: medium;">.NET Compact Framework NO VA A MORIR </span></strong>de la misma forma que no lo va a hacer <strong>Windows Mobile 6.5</strong>. Mientras <strong>WP7S</strong> se orienta hacía un claro público consumidor/productivo, <strong>WinMo 6.5</strong>, por su parte, se quedará dando cobertura al importante parque de aplicaciones empresariales/industriales. Microsoft no ha dicho oficialmente lo contrario por lo que cabe esperar tanto el soporte para WiMo 6.5 y .NET Compact Framework.</p>
<h2>Microsoft SQL Server Compact 3.5 y Visual Studio .NET 2010</h2>
<p>Con SQL Server Compact 3.1 Microsoft presentó una características importantísima a la menor de las ediciones de SQL Server al <strong><span style="text-decoration: underline;">hacerla multiplataforma</span></strong>. Esto significa que desde entonces SQL Server Compact no se liga únicamente a entornos de desarrollo para dispositivos móviles sino para lo que se denomina , aplicaciones móviles desarrolladas con <strong>WPF</strong> o <strong>Windows Forms </strong>e <strong><a href="http://erikej.blogspot.com/2010/02/access-local-sql-compact-database-from.html" target="_blank">incluso desde Silverlight</a></strong>.</p>
<p>Por lo tanto, y siguiendo la costumbre, <strong>Visual Studio 2010 .NET</strong> instala <a href="http://blogs.msdn.com/sqlservercompact/archive/2010/04/14/microsoft-sql-server-compact-3-5-with-service-pack-2-released-with-visual-studio-2010-and-on-the-web.aspx" target="_blank"><strong>MS SQL Server Compact SP2</strong> con las últimas caracterísitcas </a>en mayoria relacionadas con <a href="http://josemigueltorres.net/boresight/?tag=microsoft-sync-framework" target="_blank">MS Sync Framework</a>.</p>
<h2>Por qué Microsoft SQL Server Compact no debe morir</h2>
<ol>
<li>Porque SQL Server Compact en una –la única en Microsoft- <strong>base de datos in-process</strong>.</li>
<li>Porque SQL Server Compact es especialmente útil en aplicaciones del tipo Smart Client debido a su alta integración en entornos de sincronización o lo que se denomina base de datos caché.</li>
<li>Porque SQL Server Compact aporta una facilidad de despliegue que además es compatible con <strong>ClickOnce</strong>.</li>
<li>Porque hay escenarios en los que SQL Server Express no es el SGBD adecuado. En otros escenario no lo es SQL Server Compact.<strong> SQL Server Express NO es un rival de Compact</strong>.</li>
<li>Porque Microsoft esta adoptando SQL Server Compact como SGBD embebido.</li>
<li>Y por último y aunque sea el más trivial y menos importante, pq el <a href="http://geeks.ms/blogs/jmtorres/archive/2008/08/04/mi-primer-libro-sql-server-compact-2008.aspx" target="_blank">Bandolero se pasó más de un año</a> profundizando en el producto y materializandolo en loque fue <a href="http://shop.campusmvp.com/Product-SQL-Server-Compact-2008-SP1-Referencia-Completa_34.aspx" target="_blank">su primer libro</a>.</li>
</ol>
<h2>Conlusión</h2>
<p>En definitiva, SQL Server Compact 3.5 SP2 seguirá siendo una opción válida ante cualquier nuevo desarrollo brindando todas las características anteriormente numeradas y que la hacen realmente única.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://josemigueltorres.net/boresight/?p=402&amp;feed=comments-rss2" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://josemigueltorres.net/boresight/?p=402&amp;title=%26iexcl%3B%26iexcl%3BMicrosoft+SQL+Server+Compact+no+ha+muerto%21%21" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://josemigueltorres.net/boresight/?p=402&amp;title=%26iexcl%3B%26iexcl%3BMicrosoft+SQL+Server+Compact+no+ha+muerto%21%21" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://josemigueltorres.net/boresight/?p=402&amp;title=%26iexcl%3B%26iexcl%3BMicrosoft+SQL+Server+Compact+no+ha+muerto%21%21&amp;desc=Despu%C3%A9s%20del%20impacto%20que%20ha%20tenido%20la%20presentaci%C3%B3n%20del%20nuevo%20modelo%20de%20desarrollo%20que%20traer%C3%A1%20tras%20de%20s%C3%AD%20la%20nueva%20apuesta%20de%20Microsoft%20para%20telef%C3%B3nos%20m%C3%B3viles%20con%20Windows%20Phone%207%20Series%2C%20y%20despu%C3%A9s%20de%20que%20m%C3%A1s%20de%20uno%20se%20haya%20enfundado%20el%20traje%20de%20negro%20y%20preparado%20ya%20los%20preparativos%20para%20el%20fune" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://josemigueltorres.net/boresight/?p=402&amp;t=%26iexcl%3B%26iexcl%3BMicrosoft+SQL+Server+Compact+no+ha+muerto%21%21" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://josemigueltorres.net/boresight/?p=402&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://josemigueltorres.net/boresight/?p=402&amp;title=%26iexcl%3B%26iexcl%3BMicrosoft+SQL+Server+Compact+no+ha+muerto%21%21&amp;summary=Despu%C3%A9s%20del%20impacto%20que%20ha%20tenido%20la%20presentaci%C3%B3n%20del%20nuevo%20modelo%20de%20desarrollo%20que%20traer%C3%A1%20tras%20de%20s%C3%AD%20la%20nueva%20apuesta%20de%20Microsoft%20para%20telef%C3%B3nos%20m%C3%B3viles%20con%20Windows%20Phone%207%20Series%2C%20y%20despu%C3%A9s%20de%20que%20m%C3%A1s%20de%20uno%20se%20haya%20enfundado%20el%20traje%20de%20negro%20y%20preparado%20ya%20los%20preparativos%20para%20el%20fune&amp;source=josemiguel.torres" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://josemigueltorres.net/boresight/?p=402&amp;bm_description=%26iexcl%3B%26iexcl%3BMicrosoft+SQL+Server+Compact+no+ha+muerto%21%21&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://josemigueltorres.net/boresight/?p=402&amp;title=%26iexcl%3B%26iexcl%3BMicrosoft+SQL+Server+Compact+no+ha+muerto%21%21" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://josemigueltorres.net/boresight/?p=402&amp;title=%26iexcl%3B%26iexcl%3BMicrosoft+SQL+Server+Compact+no+ha+muerto%21%21" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://josemigueltorres.net/boresight/?p=402&amp;title=%26iexcl%3B%26iexcl%3BMicrosoft+SQL+Server+Compact+no+ha+muerto%21%21" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://josemigueltorres.net/boresight/?p=402" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=%26iexcl%3B%26iexcl%3BMicrosoft+SQL+Server+Compact+no+ha+muerto%21%21+-+http://b2l.me/pfpvw&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-yahoomail">
			<a href="http://compose.mail.yahoo.com/?Subject=%26iexcl%3B%26iexcl%3BMicrosoft+SQL+Server+Compact+no+ha+muerto%21%21&amp;body=Link: http://josemigueltorres.net/boresight/?p=402 (sent via shareaholic)%0D%0A%0D%0A----%0D%0A Despu%C3%A9s%20del%20impacto%20que%20ha%20tenido%20la%20presentaci%C3%B3n%20del%20nuevo%20modelo%20de%20desarrollo%20que%20traer%C3%A1%20tras%20de%20s%C3%AD%20la%20nueva%20apuesta%20de%20Microsoft%20para%20telef%C3%B3nos%20m%C3%B3viles%20con%20Windows%20Phone%207%20Series%2C%20y%20despu%C3%A9s%20de%20que%20m%C3%A1s%20de%20uno%20se%20haya%20enfundado%20el%20traje%20de%20negro%20y%20preparado%20ya%20los%20preparativos%20para%20el%20fune" rel="nofollow" class="external" title="Email this via Yahoo! Mail">Email this via Yahoo! Mail</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://josemigueltorres.net/boresight/?feed=rss2&amp;p=402</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://josemigueltorres.net/boresight/?p=402</feedburner:origLink></item>
		<item>
		<title>Duality of IEnumerable/IObservable</title>
		<link>http://feedproxy.google.com/~r/josemigueltorres/rXsu/~3/zjB40xR8QjA/</link>
		<comments>http://josemigueltorres.net/boresight/?p=397#comments</comments>
		<pubDate>Tue, 06 Apr 2010 10:33:30 +0000</pubDate>
		<dc:creator>josemiguel.torres</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[.NET Framework 4.0]]></category>
		<category><![CDATA[Rx]]></category>
		<category><![CDATA[Visual C#]]></category>

		<guid isPermaLink="false">http://josemigueltorres.net/boresight/?p=397</guid>
		<description><![CDATA[I’ve been spending some time with Reactive Extensions for the last months. I came across with such framework from Erik Meijer’s live session and since then, I’ve been trying to understand what Rx exactly tries to offer us as developers. Rx turns around the duality that exists between IEnumerable and IObservable. This last one, is [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been spending some time with <a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" target="_blank"><strong>Reactive Extensions</strong> </a>for the last months. I came across with such framework from Erik Meijer’s live session and since then, I’ve been trying to understand what Rx exactly tries to offer us as developers.</p>
<p><strong>Rx</strong> turns around the duality that exists between <strong>IEnumerable</strong> and <strong>IObservable</strong>. This last one, is one of the new features <strong>.NET Framework 4.0</strong> offers. I told about it in this <strong><a href="http://josemigueltorres.net/boresight/?p=369" target="_blank">post</a></strong>. Now, the idea arises on how is exactly such duality –mathematical duality, in fact- between both interfaces since it’s highly important to understand it in order to be able to apply Rx successfully to our developments. Before .NET Framework 4.0  we have been dealing objects with LINQ to Objects, so if <strong>IObservable</strong> deals with <strong>asynchronous programming model</strong> as well as <strong>push-based notifications</strong> –like events- then why not enhance LINQ for dealing with Events and async callbacks? That’s the reason Rx states.</p>
<h2>Let’s talk about IEnumerable interface.</h2>
<p><strong>IEnumerable</strong> objects just exposes enumerator represented by <strong>IEnumerator</strong> interface for supporting a simple iteration over a collection of given generic type.We’ll not be able to execute the <strong>foreach</strong> statement unless the collection implements IEnumerator, actually there wouldn’t be a way to do this without IEnumerator implementation.</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">IEnumerable&lt;Customer&gt; customers = <span style="color: #008000;">//external call or so</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">Func&lt;Customer, <span style="color: #0000ff;">bool</span>&gt; hasInvoices = <span style="color: #0000ff;">delegate</span>(Customer customer)</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                                        {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                                            <span style="color: #0000ff;">return</span> customer.Invoices.Count &gt; 0;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                                        };</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var res = customers .Where(hasInvoices);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">foreach</span> (var c <span style="color: #0000ff;">in</span> res)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    Console.WriteLine(c.Name);</pre>
</div>
</div>
<p>The example showed above represents a iteration with a collection of customers that has invoices. Whatever the type of customer object was -List&lt;T&gt; for example- it must implement <strong>IEnumerable&lt;Customer&gt;</strong> interface. IEnumerable&lt;T&gt; exposes, as said before, an <strong>IEnumerator</strong> object as we can see at <strong>IEnumerable</strong> definition:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span> IEnumerable&lt;<span style="color: #0000ff;">out</span> T&gt; : IEnumerable</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">new</span> IEnumerator&lt;T&gt; GetEnumerator();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
</div>
</div>
<p>As exposed above, an IEnumerator&lt;T&gt; object is being returned each time <strong>GetEnumerator()</strong> is called. On the other hand, IEnumerator definition is: (let me obviate generics right now)</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span> IEnumerator</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">object</span> Current { get; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">bool</span> MoveNext();</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">void</span> Reset();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
</div>
</div>
<p>Now, a couple of things we could say about this last code snippet. At first, note that just 2 of those 3 exposed methods are “<em>really necessary</em>”, <strong>Current</strong> and <strong>MoveNext</strong>. <strong>Reset</strong> method is provided for <strong>COM compatibility</strong> thus forget it.</p>
<p>For the second reason let me return to customer’s invoice sample showed above. Let’s says we are dealing with unpaid invoices – we need money!! – The extension method “Where” of customers objects filtered all the clients based on a predicate represented by <strong>hasInvoice</strong> generic function. <strong>foreach</strong> statements is iterating defaulter customers –we want their names – for contacting them. Let’s say we’ve got 4 defaulter customers so <strong>foreach</strong> statement is going to call both <strong>MoveNext</strong> and <strong>Current</strong> methods of <strong>IEnumerator</strong> interface in order to iterate <em>res</em> collection object. foreach statement asks enumerator for returning next customer –<strong>MoveNext</strong>-  and after enumerator said <strong>true</strong> –it finds and moves to the next one- such customer will be returned –via Current property -. Such process will take place 4 times -for each defaulter customer- until <strong>MoveNext</strong> method returns <strong>false</strong>. In such case, the enumerator is after the last element –last customer in our case –.</p>
<p>That’s all.That’s what IEnumerable interface really does, nothing more.</p>
<h2>All right, now introduce me duality…</h2>
<p>Thus, let’s talk about duality, mathematical duality. We can find some examples in other fields like <a href="http://en.wikipedia.org/wiki/De_Morgan's_laws" target="_blank">De Morgan’s laws</a> used in formal logic or <a href="http://en.wikipedia.org/wiki/Colimit" target="_blank">limit and colimit</a> of given function in calculus and analysis. In .NET Framework <strong>IEnumerable</strong> is <strong><a href="http://en.wikipedia.org/wiki/Dual_(category_theory)" target="_blank">dual</a></strong> with <strong>IObservable</strong>, in other words, <strong>LINQ to Objets</strong> is dual with <strong>LINQ to Events</strong>. Take a look at the following images which represents an IEnumerable function:</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image.png"><img style="display: inline; border-width: 0px;" title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image_thumb.png" border="0" alt="image" width="244" height="158" /></a></p>
<p>Image1. IEnumerable function</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image3.png"><img style="display: inline; border-width: 0px;" title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image3_thumb.png" border="0" alt="image" width="345" height="125" /></a></p>
<p>Image 2. IEnumerable function</p>
<p>Both IEnumerator and IObservable approaches deal with a collection however the main difference is how each one deals with such collection. While IEnumerable approach is continuously asking for the next object, IObservable provides such object to its subscribers. Check the following images which represents IObservable function.</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image7.png"><img style="display: inline; border-width: 0px;" title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image7_thumb.png" border="0" alt="image" width="244" height="123" /></a></p>
<p>Image 3. IObservable function</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image10.png"><img style="display: inline; border-width: 0px;" title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image10_thumb.png" border="0" alt="image" width="244" height="164" /></a></p>
<p>Image 4. IObservable function</p>
<p>If you read my post <strong><a href="http://josemigueltorres.net/boresight/?p=369" target="_blank">Observer Design Pattern with .NET Framework 4.0</a></strong> you may be familiarized with both IObservable and IObserver interfaces. Therefore, whatsoever duality of both models allows is to materialize given IEnumerable collection to IObservable one and vice versa.</p>
<p>Let’s take the IEnumerable definition again keeping in mind the functions images showed above.</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image13.png"><img style="display: inline; border-width: 0px;" title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image13_thumb.png" border="0" alt="image" width="627" height="230" /></a></p>
<p>Image 5. IEnumerable and IEnumerator definition</p>
<p>For turning IEnumerable into a IObservable, the first step is to change the duals methods. The equivalent method for IObservable would be <strong>Subscribe()</strong> instead of <strong>GetEnumerator()</strong>. GetEnumerator() gets no arguments so Subscriber will return void.</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image17.png"><img style="display: inline; border-width: 0px;" title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image17_thumb.png" border="0" alt="image" width="436" height="143" /></a></p>
<p>Image 6. IEnumerable.GetEnumerator() turns into method IObservable.Subscribe() returning void</p>
<p>GetEnumerator() returns IEnumerator&lt;T&gt; so Subscribe() will receive its equivalent by parameter.</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image21.png"><img style="display: inline; border-width: 0px;" title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image21_thumb.png" border="0" alt="image" width="442" height="141" /></a></p>
<p>Image 7. IEnumerable.GetEnumerator() turns into method IObservable.Subscribe() passing IObserver&lt;T&gt; object as parameter</p>
<p>So, by the moment we are getting the following:</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image25.png"><img style="display: inline; border-width: 0px;" title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image25_thumb.png" border="0" alt="image" width="437" height="136" /></a></p>
<p>Image 8. IObservable.Subscribe() final transformation.</p>
<p>Now, talk about IObserver –note that IObservable is not still finished –. <strong>IObserver</strong> duality is <strong>IEnumerator</strong> so let’s take it. <strong>Current</strong> property will be turned into a <strong>OnNext</strong> property right now. While <strong>Current</strong> is a getter property <strong>OnNext</strong> will be setter one.</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image29.png"><img style="display: inline; border-width: 0px;" title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image29_thumb.png" border="0" alt="image" width="441" height="86" /></a></p>
<p>Image 9. Turning IEnumerator into IObserver.</p>
<p>But what’s is actually a property but a method? therefore, the final contract for OnNext item will be:</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image33.png"><img style="display: inline; border-width: 0px;" title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image33_thumb.png" border="0" alt="image" width="449" height="99" /></a></p>
<p>Image 10. Turning IEnumerator into IObserver.</p>
<p>On the other hand MoveNext() method arises a couple of questions. Its contract tell us that it just returns <strong>bool</strong> value but it is not absolutely true. MoveNext() may throw an exception such as <strong>InvalidOperationException</strong>, for instance. So what we are getting here is two different  return types, either bool or Exception. So its duality will be actually two methods: <strong>OnError()</strong> for an exception and <strong>OnComplete()</strong> when <strong>MoveNext</strong> returns false. You may wondering about what’s happening when <strong>MoveNext()</strong> returns <strong>true</strong>. Actually, think about it carefully. Does such case concerns to subscribers? no it does not or we might say it is dual to <strong>OnNext() </strong>method. So finally will get following contracts:</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image41.png"><img style="display: inline; border-width: 0px;" title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image41_thumb.png" border="0" alt="image" width="495" height="137" /></a></p>
<p>Image 11. Turning IEnumerator into IObserver.</p>
<p>Now, both <strong>IObservable</strong> and <strong>IObserver</strong> interfaces are taking form. Withdrawing annotations from the image both interfaces will face as following:</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image45.png"><img style="display: inline; border-width: 0px;" title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image45_thumb.png" border="0" alt="image" width="546" height="232" /></a></p>
<p>Image 12. IObserver and IObservable final definition.</p>
<p>If we go one step forward we may notice that we forgot one interface in the road. We have seen how <strong>IEnumerable.GetEnumerator</strong> turns into <strong>IObservable</strong>.Subscribe() step by step. However, what previously was IEnumerator and have became an IObserver both implements IDisposable interface as well. Actually we could say that IEnumerable.GetEnumerator() could be defined as:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">IEnumerator&lt;T&gt;, IDisposable IEnumerator.GetEnumerator()</pre>
</div>
</div>
<p>So in its own duality we may turn such statement into:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">void</span> IObservable.Subscribe(IObserver&lt;T&gt; observer)</pre>
</div>
</div>
<p>Leaving no interfaces as a return type that is void. On the other hand, during such transformation we got a Subscribe() method but not UnSubscribe one. In the same way we want an observer to be subscribed to observable collection we might want to <strong>unsubscribe</strong> it. It could easily resolve by leaving <strong>IDisposable</strong> interface as returning type as follows:</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image49.png"><img style="display: inline; border-width: 0px;" title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/04/image49_thumb.png" border="0" alt="image" width="535" height="236" /></a></p>
<p>Image 13. Each IObserver object is IDisposable.</p>
<h2>Conclusions</h2>
<p>What is really interesting here beyond conceptual/theorist explanation is that any developer could understand that an event or any other push-based notification or async technology can be handled with LINQ in the same way that whatever IEnumerable oject does. Silverlight, WPF, SQL Server Integration Services are just an examples of technologies that uses asynchronous callbacks.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://josemigueltorres.net/boresight/?p=397&amp;feed=comments-rss2" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://josemigueltorres.net/boresight/?p=397&amp;title=Duality+of+IEnumerable%2FIObservable+" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://josemigueltorres.net/boresight/?p=397&amp;title=Duality+of+IEnumerable%2FIObservable+" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://josemigueltorres.net/boresight/?p=397&amp;title=Duality+of+IEnumerable%2FIObservable+&amp;desc=I%E2%80%99ve%20been%20spending%20some%20time%20with%20Reactive%20Extensions%20for%20the%20last%20months.%20I%20came%20across%20with%20such%20framework%20from%20Erik%20Meijer%E2%80%99s%20live%20session%20and%20since%20then%2C%20I%E2%80%99ve%20been%20trying%20to%20understand%20what%20Rx%20exactly%20tries%20to%20offer%20us%20as%20developers.%0D%0A%0D%0ARx%20turns%20around%20the%20duality%20that%20exists%20between%20IEnume" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://josemigueltorres.net/boresight/?p=397&amp;t=Duality+of+IEnumerable%2FIObservable+" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://josemigueltorres.net/boresight/?p=397&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://josemigueltorres.net/boresight/?p=397&amp;title=Duality+of+IEnumerable%2FIObservable+&amp;summary=I%E2%80%99ve%20been%20spending%20some%20time%20with%20Reactive%20Extensions%20for%20the%20last%20months.%20I%20came%20across%20with%20such%20framework%20from%20Erik%20Meijer%E2%80%99s%20live%20session%20and%20since%20then%2C%20I%E2%80%99ve%20been%20trying%20to%20understand%20what%20Rx%20exactly%20tries%20to%20offer%20us%20as%20developers.%0D%0A%0D%0ARx%20turns%20around%20the%20duality%20that%20exists%20between%20IEnume&amp;source=josemiguel.torres" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://josemigueltorres.net/boresight/?p=397&amp;bm_description=Duality+of+IEnumerable%2FIObservable+&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://josemigueltorres.net/boresight/?p=397&amp;title=Duality+of+IEnumerable%2FIObservable+" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://josemigueltorres.net/boresight/?p=397&amp;title=Duality+of+IEnumerable%2FIObservable+" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://josemigueltorres.net/boresight/?p=397&amp;title=Duality+of+IEnumerable%2FIObservable+" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://josemigueltorres.net/boresight/?p=397" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Duality+of+IEnumerable%2FIObservable++-+http://b2l.me/nga2q&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-yahoomail">
			<a href="http://compose.mail.yahoo.com/?Subject=Duality+of+IEnumerable%2FIObservable+&amp;body=Link: http://josemigueltorres.net/boresight/?p=397 (sent via shareaholic)%0D%0A%0D%0A----%0D%0A I%E2%80%99ve%20been%20spending%20some%20time%20with%20Reactive%20Extensions%20for%20the%20last%20months.%20I%20came%20across%20with%20such%20framework%20from%20Erik%20Meijer%E2%80%99s%20live%20session%20and%20since%20then%2C%20I%E2%80%99ve%20been%20trying%20to%20understand%20what%20Rx%20exactly%20tries%20to%20offer%20us%20as%20developers.%0D%0A%0D%0ARx%20turns%20around%20the%20duality%20that%20exists%20between%20IEnume" rel="nofollow" class="external" title="Email this via Yahoo! Mail">Email this via Yahoo! Mail</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://josemigueltorres.net/boresight/?feed=rss2&amp;p=397</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://josemigueltorres.net/boresight/?p=397</feedburner:origLink></item>
		<item>
		<title>Observer Design Pattern with .NET</title>
		<link>http://feedproxy.google.com/~r/josemigueltorres/rXsu/~3/-Hjt__1zKwE/</link>
		<comments>http://josemigueltorres.net/boresight/?p=369#comments</comments>
		<pubDate>Fri, 26 Feb 2010 12:40:07 +0000</pubDate>
		<dc:creator>josemiguel.torres</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[.NET Framework 4.0]]></category>
		<category><![CDATA[Visual C#]]></category>
		<category><![CDATA[Visual Studio .NET 2010]]></category>

		<guid isPermaLink="false">http://josemigueltorres.net/boresight/?p=369</guid>
		<description><![CDATA[.NET Framework 4.0 offers a set of new features focused on Observable/Observer design pattern. Actually, such pattern is a subset of Publish/Subscribe design pattern. Such kind of patterns aim to provide a mechanism for push-based notifications. The idea turns around two new generic interfaces IObserver&#60;T&#62;  and IObservable&#60;T&#62; . Whilst IObservable&#60;T&#62; provides all the functionality for the [...]]]></description>
			<content:encoded><![CDATA[<p><strong>.NET Framework 4.0</strong> offers a set of new features focused on <strong>Observable/Observer design pattern</strong>. Actually, such pattern is a subset of Publish/Subscribe design pattern. Such kind of patterns aim to provide a mechanism for push-based notifications.</p>
<p>The idea turns around two new generic interfaces <a href="http://msdn.microsoft.com/en-us/library/dd783449(VS.100).aspx" target="_blank">IObserver&lt;T&gt;</a><strong> </strong> and <a href="http://msdn.microsoft.com/en-us/library/dd990377(VS.100).aspx" target="_blank">IObservable&lt;T&gt;</a><strong> </strong>. Whilst <strong>IObservable&lt;T&gt;</strong> provides all the functionality for the publisher, <strong>IObserver&lt;T&gt;</strong> does the same for subscriber. You will find that publisher and subscriber are also known as provider and observer. Whatever name is used for, keep in mind what each item is supposed to do, otherwise you will feel confused.</p>
<ul>
<li>Publisher –&gt; Provider –&gt; Observable</li>
<li>Subscriber –&gt; Observer</li>
</ul>
<p>Notice that both interfaces are generics by &lt;T&gt;. Actually, <strong>IObservable</strong> is covariant with &lt;T&gt; whereas <strong>IObserver</strong> is contravariant with &lt;T&gt;. The reason is that whilst <strong>IObservable</strong> could provide push-based notification methods to the indicated type &lt;T&gt; and more derived &lt;T&gt; types, the <strong>IObserver</strong> only could observe such derived types from &lt;T’&gt; to &lt;T&gt; where &lt;T’&gt; is the derived type of &lt;T&gt;, i.e. <strong>IObserver</strong> could use &lt;T&gt; and less derived types. The contracts of both interfaces are declared as follows:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span> IObserver&lt;<span style="color: #0000ff;">in</span> T&gt;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">     <span style="color: #0000ff;">void</span> OnCompleted();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">     <span style="color: #0000ff;">void</span> OnError(Exception error);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">     <span style="color: #0000ff;">void</span> OnNext(T <span style="color: #0000ff;">value</span>);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
</div>
</div>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span> IObservable&lt;<span style="color: #0000ff;">out</span> T&gt;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    IDisposable Subscribe(IObserver&lt;T&gt; observer);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
</div>
</div>
<h2>I wish to be observed</h2>
<p>That said, the main target of an observable object is to be observed by a bunch of observers. Observable owns just one method used for subscribing in an observer. On the other hand, each time an observable object push a new notification it would be caught by <strong>OnNext</strong> observer’s method. Let’s see an example.</p>
<p>Suppose we have a simply class called Car which describes a racing car.</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">class</span> Car</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #008000;">//common properties</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> Name { get; set; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Id { get; set; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Position { get; set; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">long</span> BestTime { get; <span style="color: #0000ff;">private</span> set; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">long</span> LastTime { get; <span style="color: #0000ff;">private</span> set; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">public</span> TimeSpan TotalTime { get; set; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">public</span> Car(<span style="color: #0000ff;">string</span> name, <span style="color: #0000ff;">int</span> id)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        Name = name;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        Id = id;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        BestTime = <span style="color: #0000ff;">long</span>.MaxValue;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        TotalTime = <span style="color: #0000ff;">new</span> TimeSpan();</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #008000;">//use this method for updating stats</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> LastLap(<span style="color: #0000ff;">long</span> lastlaptime)</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #008000;">//saving the last lap time</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        LastTime = lastlaptime;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #008000;">//is the lastlap the best time?</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        BestTime = (lastlaptime &lt; BestTime) ? lastlaptime : BestTime;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #008000;">//total time</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        TotalTime = TotalTime.Add(<span style="color: #0000ff;">new</span> TimeSpan(lastlaptime));</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">string</span> ToString()</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">string</span>.Format(<span style="color: #006080;">"Car {0} -\t TotalTime: {3}\n- LastTime: {1}\n- BestTime: {2}"</span>,</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #0000ff;">this</span>.Id.ToString(<span style="color: #006080;">"00"</span>), <span style="color: #0000ff;">new</span> TimeSpan(<span style="color: #0000ff;">this</span>.LastTime),</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #0000ff;">new</span> TimeSpan(<span style="color: #0000ff;">this</span>.BestTime), <span style="color: #0000ff;">this</span>.TotalTime);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
</div>
</div>
<p>Besides, a simply class which describes a Race.</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   1:</span> <span style="color: #0000ff;">class</span> Race</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   2:</span> {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   3:</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> TotalLaps { get; set; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   4:</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> CurrentLap { get; set; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   5:</span>     <span style="color: #0000ff;">public</span> List&lt;Car&gt; Cars { get; set; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   6:</span> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   7:</span>     <span style="color: #0000ff;">public</span> Race(<span style="color: #0000ff;">int</span> totalLaps)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   8:</span>     {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   9:</span>         Cars = <span style="color: #0000ff;">new</span> List&lt;Car&gt;();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  10:</span>         TotalLaps = totalLaps;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  11:</span>     }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  12:</span> }</pre>
</div>
</div>
<p>Then, our goal is monitoring a car racing. Let’s say that such car racing is Nascar which will contain a Race type object and such Race object will contain a generic list of participants Cars, as well. What we aim to monitor is the Nascar car racing so we may create a class NascarRace which will be observable for car racing journalist, let’s say NBC and BBC. Thus, such journalist would became nascar’s observer.</p>
<p>The nascar race could be described as follows:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">class</span> NascarRace : IObservable&lt;Race&gt;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    List&lt;IObserver&lt;Race&gt;&gt; display = <span style="color: #0000ff;">new</span> List&lt;IObserver&lt;Race&gt;&gt;();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    Race race = <span style="color: #0000ff;">new</span> Race(400);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> Start()</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #008000;">//start up cars</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        Car car1 = <span style="color: #0000ff;">new</span> Car(<span style="color: #006080;">"Car1"</span>, 1);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        Car car2 = <span style="color: #0000ff;">new</span> Car(<span style="color: #006080;">"Car2"</span>, 46);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        Car car3 = <span style="color: #0000ff;">new</span> Car(<span style="color: #006080;">"Car3"</span>, 99);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        race.Cars.Add(car1);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        race.Cars.Add(car2);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        race.Cars.Add(car3);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        ThreadStart threadDelegate = <span style="color: #0000ff;">new</span> ThreadStart(Running);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        Thread newThread = <span style="color: #0000ff;">new</span> Thread(threadDelegate);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        newThread.Start();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> Running()</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #008000;">//lap loop</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> lap = 0; lap &lt; race.TotalLaps; lap++)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            Random rnd = <span style="color: #0000ff;">new</span> Random();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            race.Cars.ToList().ForEach(r =&gt;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                    <span style="color: #008000;">//setting the last lap time randomly</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                    r.LastLap(rnd.Next(1000000000, <span style="color: #0000ff;">int</span>.MaxValue));</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                });</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #008000;">//increasing num of laps done</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            race.CurrentLap++;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #008000;">//observers notification</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            display.ToList().ForEach(d =&gt; d.OnNext(race));</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #008000;">//waiting for a second</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            Thread.Sleep(1000);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #008000;">//completed. observers notification</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        display.ToList().ForEach(d =&gt; d.OnCompleted());</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">public</span> IDisposable Subscribe(IObserver&lt;Race&gt; observer)</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #008000;">//new observer</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        display.Add(observer);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">return</span> observer <span style="color: #0000ff;">as</span> IDisposable;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
</div>
</div>
<p>Note that NascarRace class constructor sets up three cars which are going to participate to the competition. On the other hand a new thread is created and launched once the competition starts up. ThreadStart delegate points to a method called Running() and this method will hold the racing timeline.  The total laps are set by default at 40 so during 40 laps all three cars will be gathering elapsed time and for each lap all the observers are going to be notified. RaceMonitor will be displaying race results in ascending order. Before next lap, thread gets slept for a 1 second in order to give more emotion to simulated nascar racing <img src='http://jtorresd.brinkster.net/boresight/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<h2>Now, I wish to observe</h2>
<p>On the other hand journalist or anyone else whom aims to be an observer would be described through RaceMonitor class described as follows:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   1:</span> <span style="color: #0000ff;">class</span> RaceMonitor : IObserver&lt;Race&gt;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   2:</span> {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   3:</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> OnCompleted()</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   4:</span>     {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   5:</span>         Console.WriteLine(<span style="color: #006080;">"Finished"</span>);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   6:</span>     }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   7:</span> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   8:</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> OnError(Exception error)</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   9:</span>     {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  10:</span>         Console.Write(error.Message);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  11:</span>     }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  12:</span> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  13:</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> OnNext(Race <span style="color: #0000ff;">value</span>)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  14:</span>     {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  15:</span>         Console.Clear();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  16:</span>         Console.WriteLine(<span style="color: #0000ff;">string</span>.Format(<span style="color: #006080;">"Race Lap {0}"</span>, <span style="color: #0000ff;">value</span>.CurrentLap));</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  17:</span>         <span style="color: #0000ff;">value</span>.Cars.OrderBy(c =&gt; c.TotalTime)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  18:</span>             .ToList()</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  19:</span>             .ForEach(car =&gt; Console.WriteLine(car.ToString()));</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  20:</span>     }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  21:</span> }</pre>
</div>
</div>
<p>The observers will be represented by RaceMonitor class and will be subscribed to NascarRace through Subscribe() method in the main method’s program.</p>
<p>The main program would be:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">class</span> Program</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span>[] args)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        NascarRace nascarRace = <span style="color: #0000ff;">new</span> NascarRace();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #008000;">//observer</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        RaceMonitor nbssports = <span style="color: #0000ff;">new</span> RaceMonitor();</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        RaceMonitor bbcsports = <span style="color: #0000ff;">new</span> RaceMonitor();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        nascarRace.Subscribe(nbssports);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        nascarRace.Subscribe(bbcsports);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #008000;">//let race start</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        nascarRace.Start();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        Console.Read();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
</div>
</div>
<p>And the final standings!!</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/02/image2.png"><img title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/02/image_thumb2.png" border="0" alt="image" width="515" height="262" /></a></p>
<h2>Call’s flow?</h2>
<p>Check how push notifications to <strong>IObserver&lt;Race&gt; </strong>object are made at following sequence diagram of NascarRace.Running method. All  take place into lap control loop.</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/02/image3.png"><img style="display: inline; border-width: 0px;" title="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/02/image_thumb3.png" border="0" alt="image" width="813" height="539" /></a></p>
<p>Deferred calls are made since we are actually using LINQ’s method extension for iterating both Cars and Observers (RaceMonitor objects).</p>
<p>Here you are complete code listing:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">using</span> System;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">using</span> System.Collections.Generic;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">using</span> System.Linq;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">using</span> System.Text;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">using</span> System.Threading;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">namespace</span> Observables</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">class</span> Program</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span>[] args)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            NascarRace nascarRace = <span style="color: #0000ff;">new</span> NascarRace();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #008000;">//observer</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            RaceMonitor nbssports = <span style="color: #0000ff;">new</span> RaceMonitor();</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            RaceMonitor bbcsports = <span style="color: #0000ff;">new</span> RaceMonitor();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            nascarRace.Subscribe(nbssports);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            nascarRace.Subscribe(bbcsports);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #008000;">//let race start</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            nascarRace.Start();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            Console.Read();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">class</span> Car</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #008000;">//common properties</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> Name { get; set; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Id { get; set; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Position { get; set; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">long</span> BestTime { get; <span style="color: #0000ff;">private</span> set; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">long</span> LastTime { get; <span style="color: #0000ff;">private</span> set; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> TimeSpan TotalTime { get; set; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> Car(<span style="color: #0000ff;">string</span> name, <span style="color: #0000ff;">int</span> id)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            Name = name;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            Id = id;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            BestTime = <span style="color: #0000ff;">long</span>.MaxValue;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            TotalTime = <span style="color: #0000ff;">new</span> TimeSpan();</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #008000;">//use this method for updating stats</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> LastLap(<span style="color: #0000ff;">long</span> lastlaptime)</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #008000;">//saving the last lap time</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            LastTime = lastlaptime;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #008000;">//is the lastlap the best time?</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            BestTime = (lastlaptime &lt; BestTime) ? lastlaptime : BestTime;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #008000;">//total time</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            TotalTime = TotalTime.Add(<span style="color: #0000ff;">new</span> TimeSpan(lastlaptime));</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">string</span> ToString()</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">string</span>.Format(<span style="color: #006080;">"Car {0} -\t TotalTime: {3}\n- LastTime: {1}\n- BestTime: {2}"</span>,</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                <span style="color: #0000ff;">this</span>.Id.ToString(<span style="color: #006080;">"00"</span>), <span style="color: #0000ff;">new</span> TimeSpan(<span style="color: #0000ff;">this</span>.LastTime),</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                <span style="color: #0000ff;">new</span> TimeSpan(<span style="color: #0000ff;">this</span>.BestTime), <span style="color: #0000ff;">this</span>.TotalTime);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">class</span> Race</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> TotalLaps { get; set; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> CurrentLap { get; set; }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> List&lt;Car&gt; Cars { get; set; }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> Race(<span style="color: #0000ff;">int</span> totalLaps)</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            Cars = <span style="color: #0000ff;">new</span> List&lt;Car&gt;();</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            TotalLaps = totalLaps;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">class</span> RaceMonitor : IObserver&lt;Race&gt;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> OnCompleted()</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            Console.WriteLine(<span style="color: #006080;">"Finished"</span>);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> OnError(Exception error)</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            Console.Write(error.Message);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> OnNext(Race <span style="color: #0000ff;">value</span>)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            Console.Clear();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            Console.WriteLine(<span style="color: #0000ff;">string</span>.Format(<span style="color: #006080;">"Race Lap {0}"</span>, <span style="color: #0000ff;">value</span>.CurrentLap));</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #0000ff;">value</span>.Cars.OrderBy(c =&gt; c.TotalTime)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                .ToList()</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                .ForEach(car =&gt; Console.WriteLine(car.ToString()));</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">class</span> NascarRace : IObservable&lt;Race&gt;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        List&lt;IObserver&lt;Race&gt;&gt; display = <span style="color: #0000ff;">new</span> List&lt;IObserver&lt;Race&gt;&gt;();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        Race race = <span style="color: #0000ff;">new</span> Race(40);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> Start()</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #008000;">//start up cars</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            Car car1 = <span style="color: #0000ff;">new</span> Car(<span style="color: #006080;">"Car1"</span>, 1);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            Car car2 = <span style="color: #0000ff;">new</span> Car(<span style="color: #006080;">"Car2"</span>, 46);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            Car car3 = <span style="color: #0000ff;">new</span> Car(<span style="color: #006080;">"Car3"</span>, 99);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            race.Cars.Add(car1);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            race.Cars.Add(car2);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            race.Cars.Add(car3);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            ThreadStart threadDelegate = <span style="color: #0000ff;">new</span> ThreadStart(Running);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            Thread newThread = <span style="color: #0000ff;">new</span> Thread(threadDelegate);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            newThread.Start();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> Running()</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #008000;">//lap loop</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> lap = 0; lap &lt; race.TotalLaps; lap++)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                Random rnd = <span style="color: #0000ff;">new</span> Random();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                race.Cars.ToList().ForEach(r =&gt;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                    {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                        <span style="color: #008000;">//setting the last lap time randomly</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                        r.LastLap(rnd.Next(1000000000, <span style="color: #0000ff;">int</span>.MaxValue));</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                    });</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                <span style="color: #008000;">//increasing num of laps done</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                race.CurrentLap++;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                <span style="color: #008000;">//observers notification</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                display.ToList().ForEach(d =&gt; d.OnNext(race));</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                <span style="color: #008000;">//waiting for a second</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">                Thread.Sleep(1000);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #008000;">//completed. observers notification</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            display.ToList().ForEach(d =&gt; d.OnCompleted());</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        <span style="color: #0000ff;">public</span> IDisposable Subscribe(IObserver&lt;Race&gt; observer)</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #008000;">//new observer</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            display.Add(observer);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">            <span style="color: #0000ff;">return</span> observer <span style="color: #0000ff;">as</span> IDisposable;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
</div>
</div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://josemigueltorres.net/boresight/?p=369&amp;feed=comments-rss2" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://josemigueltorres.net/boresight/?p=369&amp;title=Observer+Design+Pattern+with+.NET" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://josemigueltorres.net/boresight/?p=369&amp;title=Observer+Design+Pattern+with+.NET" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://josemigueltorres.net/boresight/?p=369&amp;title=Observer+Design+Pattern+with+.NET&amp;desc=.NET%20Framework%204.0%20offers%20a%20set%20of%20new%20features%20focused%20on%20Observable%2FObserver%20design%20pattern.%20Actually%2C%20such%20pattern%20is%20a%20subset%20of%20Publish%2FSubscribe%20design%20pattern.%20Such%20kind%20of%20patterns%20aim%20to%20provide%20a%20mechanism%20for%20push-based%20notifications.%0D%0A%0D%0AThe%20idea%20turns%20around%20two%20new%20generic%20interfaces%20IO" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://josemigueltorres.net/boresight/?p=369&amp;t=Observer+Design+Pattern+with+.NET" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://josemigueltorres.net/boresight/?p=369&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://josemigueltorres.net/boresight/?p=369&amp;title=Observer+Design+Pattern+with+.NET&amp;summary=.NET%20Framework%204.0%20offers%20a%20set%20of%20new%20features%20focused%20on%20Observable%2FObserver%20design%20pattern.%20Actually%2C%20such%20pattern%20is%20a%20subset%20of%20Publish%2FSubscribe%20design%20pattern.%20Such%20kind%20of%20patterns%20aim%20to%20provide%20a%20mechanism%20for%20push-based%20notifications.%0D%0A%0D%0AThe%20idea%20turns%20around%20two%20new%20generic%20interfaces%20IO&amp;source=josemiguel.torres" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://josemigueltorres.net/boresight/?p=369&amp;bm_description=Observer+Design+Pattern+with+.NET&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://josemigueltorres.net/boresight/?p=369&amp;title=Observer+Design+Pattern+with+.NET" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://josemigueltorres.net/boresight/?p=369&amp;title=Observer+Design+Pattern+with+.NET" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://josemigueltorres.net/boresight/?p=369&amp;title=Observer+Design+Pattern+with+.NET" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://josemigueltorres.net/boresight/?p=369" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Observer+Design+Pattern+with+.NET+-+http://b2l.me/he35r&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-yahoomail">
			<a href="http://compose.mail.yahoo.com/?Subject=Observer+Design+Pattern+with+.NET&amp;body=Link: http://josemigueltorres.net/boresight/?p=369 (sent via shareaholic)%0D%0A%0D%0A----%0D%0A .NET%20Framework%204.0%20offers%20a%20set%20of%20new%20features%20focused%20on%20Observable%2FObserver%20design%20pattern.%20Actually%2C%20such%20pattern%20is%20a%20subset%20of%20Publish%2FSubscribe%20design%20pattern.%20Such%20kind%20of%20patterns%20aim%20to%20provide%20a%20mechanism%20for%20push-based%20notifications.%0D%0A%0D%0AThe%20idea%20turns%20around%20two%20new%20generic%20interfaces%20IO" rel="nofollow" class="external" title="Email this via Yahoo! Mail">Email this via Yahoo! Mail</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://josemigueltorres.net/boresight/?feed=rss2&amp;p=369</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://josemigueltorres.net/boresight/?p=369</feedburner:origLink></item>
		<item>
		<title>Virtualización de Servicios con Managed Services Engines</title>
		<link>http://feedproxy.google.com/~r/josemigueltorres/rXsu/~3/OehtCBXxhlE/</link>
		<comments>http://josemigueltorres.net/boresight/?p=357#comments</comments>
		<pubDate>Thu, 04 Feb 2010 08:00:22 +0000</pubDate>
		<dc:creator>josemiguel.torres</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Managed Services Engine]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://josemigueltorres.net/boresight/?p=357</guid>
		<description><![CDATA[Desde mis primeras incursiones en el desarrollo de Servicios empresariales, básicamente desde la aparición de las primeras betas de WCF, ha habido un miedo escénico que me ha atormentado y es la programación déspota e incontrolada de servicios, servicios y más servicios, lo que deriva en la producción de 300 servicios, con 300 enlaces distintos [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">Desde mis primeras incursiones en el desarrollo de Servicios empresariales, básicamente desde la aparición de las primeras betas de <strong>WCF</strong>, ha habido un miedo escénico que me ha atormentado y es la programación déspota e incontrolada de servicios, servicios y más servicios, lo que deriva en la producción de 300 servicios, con 300 enlaces distintos (no más de 10 comunes), en 50 endpoints y cuyo valor operativo era equivalente a 50 servicios bien diseñados y gobernados.&#160; No concebía una arquitectura sólida de servicios sin una <em>“torre de control</em>” o una “<em>cabina de mando</em>” desde dónde poder ver, gestionar y modificar nuestros servicios. </p>
<p align="justify">Más adelante tuve la oportunidad de trabajar en una arquitectura <strong>SOA</strong> desarrollada en <strong>J2EE</strong>, dirigida por un ESB de <strong>Oracle</strong> y gestionada con herramientas de gobernabilidad SOA, que manejaban más de un centenar de servicios (<em>proxy</em> y <em>legacy</em>) y dónde la publicación y consumo de dichos servicios se realizaban a través de estrictas normas protocolarias (seguridad, documentación,…).</p>
<p align="justify">Ahora vuelvo, de nuevo, a tener la misma necesidad, diseñar una infraestructura de servicios y cuando hablo de infraestructura hablo de ese tipo de herramientas fundamentales para el control de todos y cada uno de los servicios. Pero ahora (en realidad hace ya un tiempo que existe) me encontré con un software, bajo licencia&#160; <em>Microsoft Public License</em> (Ms-PL) y código abierto, que adopta de forma práctica la idea de Virtualización de Servicios, llamado <strong><a href="http://www.codeplex.com/servicesengine">Managed Services Engine</a></strong>.</p>
<h1 align="justify">Virtualización de Servicios</h1>
<p align="justify">La idea básica del <strong>patrón de Virtualización de Servicios</strong> es el de aislar la complejidad de los servicios expuestos del cliente que los consume ya que&#160; tras cada servicio se alberga una gran cantidad de aspectos tales como las localizaciones de los endpoints, las configuraciones de los enlaces, la aplicación de políticas, etc. Además muchas de las adopciones SOA a la practica no ofrecen soluciones sobre versionado de servicios, aplicación de políticas de seguridad o cambios operativos sin necesidad volver a codificar el servicio así como el cumplimiento del <strong><a href="http://es.wikipedia.org/wiki/Acuerdo_de_nivel_de_servicio">SLA</a></strong> entre el proveedor –nosotros- y el cliente.</p>
<p align="justify">Aparece la idea de <strong>Servicio Intermediario</strong> que desacopla el cliente de la implementación del servicio. Como tal, podemos ofrece varios servicios virtuales de una misma implementación para, por ejemplo, utilizarlo en distintos escenarios. Es aquí dónde encontramos la clave de la virtualización, en el servicio intermediario, pues todas las llamadas se realizaran a través de este y podremos modificar su comportamiento sin comprometer los modelos del servicio.</p>
<p align="justify">&#160;</p>
<h1 align="justify">Managed Services Engine</h1>
<p align="justify"><strong><a href="http://www.microsoft.com/soa/solutions/soi.aspx">Microsoft Services SOA Infraestructure</a></strong> ofrece una solución de virtualización de servicios a través de <strong>Managed Services Engine (MSE</strong>). Como cabe esperar, MSE está basado en la plataforma <strong>Windows</strong>, esto es, <strong>Windows Server 2003/2008</strong>, <strong>SQL Server 2005/2008</strong> y <strong>.NET Framework 3.5</strong>, especialmente con <strong>Windows Communication Foundation</strong> para la interceptación de comunicaciones entre servicios virtuales y reales.</p>
<p align="justify">MSE puede integrarse tanto con <strong>MS Biztalk Server 2006 R2 / 2009 </strong>para proporcionar capacidades adicionales tales como la monitorización de las actividades de negocio (BAM), el Business Rules Engine o el ESB Toolkit, entre otros. Además también podemos utilizar los servicios <strong>Azure</strong> a través de Azure AppFabric, especialmente con <strong>.NET Service Bus</strong>.</p>
<p align="justify"><img style="display: inline; margin-left: 0px; margin-right: 0px" src="http://i.msdn.microsoft.com/dd727511.fig11_L(en-us).gif" width="362" height="276" /></p>
<p align="justify">MSE consiste, básicamente, en tres componentes:</p>
<ul>
<li>
<div align="justify"><em>Messenger</em>: proporciona la normalización del mensaje de entrada a través de los servicios virtuales. Este componente soporta además la aplicación de políticas (de transformación por ejemplo, tanto de peticiones como de respuestas) así como el mapeo de protocolos.</div>
</li>
<li>
<div align="justify"><em>Broker</em>: este componente obtiene el mensaje normalizado y lo reconvierte a la operación (es decir la implementación de un método del servicio) y su respectiva versión (pues podemos tener más de una operación con distintas versiones).</div>
</li>
<li>
<div align="justify"><em>Dispatcher</em>: una vez se tiene el mensaje y la operación pertinente, dispatcher invoca el método del servicio y se transmite dicho mensaje.</div>
</li>
</ul>
<p align="justify"><img alt="" src="http://i.msdn.microsoft.com/dd727511.fig02_L(ja-jp).gif" width="459" height="301" /></p>
<p align="justify">Recalcar que estos tres componentes están <strong>totalmente desacoplados</strong> unos de otros con lo que podríamos distribuirlos de forma que obtendríamos una gran cantidad de tipologías del sistema. Todo esto es gracias a la catalogo del servicio (<strong>Service Catalog</strong>), también conocido como repositorio de metadata o simplemente repository o repositorio, ya que contiene todos los modelos de los servicios que hospeda el runtime del MSE –incorpora asistentes para la importación de servicios a través del WSDL y otros mecanismos para servicios POX o REST-. Como dije anteriormente, MSE contiene una implementación de WCF así que es fácil intuir que la comunicación entre ellos se realizan a través de <strong>canales de mensajería</strong>. El repositorio, por último, utiliza una base de datos <strong>SQL Server </strong>y la información puede ser publicada en un <strong>registry </strong>externo UDDI 2.0/3.0. </p>
<p align="justify">Por último, MSE contiene una interfaz para la administración de los servicios –<strong>MSE Model Viewer</strong>- así como una herramienta de test –<strong>MSE Universal Service Tester</strong>-.</p>
<p align="justify"><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/02/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/02/image_thumb.png" width="244" height="175" /></a> </p>
<p align="justify"><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/02/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/02/image_thumb1.png" width="244" height="244" /></a> </p>
<p align="justify">En próximos posts hablaré de las diferentes posibilidades que ofrece MSE desde el punto del vista del rol (developer, IT, architect,…) y como familiarizarse MSE.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://josemigueltorres.net/boresight/?p=357&amp;feed=comments-rss2" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://josemigueltorres.net/boresight/?p=357&amp;title=Virtualizaci%26oacute%3Bn+de+Servicios+con+Managed+Services+Engines" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://josemigueltorres.net/boresight/?p=357&amp;title=Virtualizaci%26oacute%3Bn+de+Servicios+con+Managed+Services+Engines" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://josemigueltorres.net/boresight/?p=357&amp;title=Virtualizaci%26oacute%3Bn+de+Servicios+con+Managed+Services+Engines&amp;desc=Desde%20mis%20primeras%20incursiones%20en%20el%20desarrollo%20de%20Servicios%20empresariales%2C%20b%C3%A1sicamente%20desde%20la%20aparici%C3%B3n%20de%20las%20primeras%20betas%20de%20WCF%2C%20ha%20habido%20un%20miedo%20esc%C3%A9nico%20que%20me%20ha%20atormentado%20y%20es%20la%20programaci%C3%B3n%20d%C3%A9spota%20e%20incontrolada%20de%20servicios%2C%20servicios%20y%20m%C3%A1s%20servicios%2C%20lo%20que%20deriva%20en%20la%20pr" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://josemigueltorres.net/boresight/?p=357&amp;t=Virtualizaci%26oacute%3Bn+de+Servicios+con+Managed+Services+Engines" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://josemigueltorres.net/boresight/?p=357&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://josemigueltorres.net/boresight/?p=357&amp;title=Virtualizaci%26oacute%3Bn+de+Servicios+con+Managed+Services+Engines&amp;summary=Desde%20mis%20primeras%20incursiones%20en%20el%20desarrollo%20de%20Servicios%20empresariales%2C%20b%C3%A1sicamente%20desde%20la%20aparici%C3%B3n%20de%20las%20primeras%20betas%20de%20WCF%2C%20ha%20habido%20un%20miedo%20esc%C3%A9nico%20que%20me%20ha%20atormentado%20y%20es%20la%20programaci%C3%B3n%20d%C3%A9spota%20e%20incontrolada%20de%20servicios%2C%20servicios%20y%20m%C3%A1s%20servicios%2C%20lo%20que%20deriva%20en%20la%20pr&amp;source=josemiguel.torres" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://josemigueltorres.net/boresight/?p=357&amp;bm_description=Virtualizaci%26oacute%3Bn+de+Servicios+con+Managed+Services+Engines&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://josemigueltorres.net/boresight/?p=357&amp;title=Virtualizaci%26oacute%3Bn+de+Servicios+con+Managed+Services+Engines" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://josemigueltorres.net/boresight/?p=357&amp;title=Virtualizaci%26oacute%3Bn+de+Servicios+con+Managed+Services+Engines" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://josemigueltorres.net/boresight/?p=357&amp;title=Virtualizaci%26oacute%3Bn+de+Servicios+con+Managed+Services+Engines" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://josemigueltorres.net/boresight/?p=357" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Virtualizaci%26oacute%3Bn+de+Servicios+con+Managed+Services+Engines+-+http://b2l.me/fg6gA&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-yahoomail">
			<a href="http://compose.mail.yahoo.com/?Subject=Virtualizaci%26oacute%3Bn+de+Servicios+con+Managed+Services+Engines&amp;body=Link: http://josemigueltorres.net/boresight/?p=357 (sent via shareaholic)%0D%0A%0D%0A----%0D%0A Desde%20mis%20primeras%20incursiones%20en%20el%20desarrollo%20de%20Servicios%20empresariales%2C%20b%C3%A1sicamente%20desde%20la%20aparici%C3%B3n%20de%20las%20primeras%20betas%20de%20WCF%2C%20ha%20habido%20un%20miedo%20esc%C3%A9nico%20que%20me%20ha%20atormentado%20y%20es%20la%20programaci%C3%B3n%20d%C3%A9spota%20e%20incontrolada%20de%20servicios%2C%20servicios%20y%20m%C3%A1s%20servicios%2C%20lo%20que%20deriva%20en%20la%20pr" rel="nofollow" class="external" title="Email this via Yahoo! Mail">Email this via Yahoo! Mail</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://josemigueltorres.net/boresight/?feed=rss2&amp;p=357</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://josemigueltorres.net/boresight/?p=357</feedburner:origLink></item>
		<item>
		<title>Inicialización con System.Lazy en .NET Framework 4.0</title>
		<link>http://feedproxy.google.com/~r/josemigueltorres/rXsu/~3/zv9MuG6KI-Q/</link>
		<comments>http://josemigueltorres.net/boresight/?p=351#comments</comments>
		<pubDate>Thu, 21 Jan 2010 12:12:17 +0000</pubDate>
		<dc:creator>josemiguel.torres</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[.NET Framework 4.0]]></category>
		<category><![CDATA[Visual C#]]></category>
		<category><![CDATA[Visual Studio .NET 2010]]></category>

		<guid isPermaLink="false">http://josemigueltorres.net/boresight/?p=351</guid>
		<description><![CDATA[Una de las novedades de .NET Framework 4.0 es la presentación de la clase System.Lazy&#60;T&#62;. En programación, una inicialización Lazy (el término Lazy significa, literalmente, gandul o retardado) es un método de inicialización que permite que el objeto no se instancie hasta que se llame por primera vez, es decir hasta que sea utilizado. En [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">Una de las novedades de .NET Framework 4.0 es la presentación de la clase <a href="http://msdn.microsoft.com/en-us/library/dd642331(VS.100).aspx" target="_blank">System.Lazy&lt;T&gt;</a>. En programación, una <a href="http://en.wikipedia.org/wiki/Lazy_initialization" target="_blank">inicialización Lazy</a> (el término <strong>Lazy</strong> significa, literalmente, <strong>gandul</strong> o <strong>retardado</strong>) es un método de inicialización que permite que el objeto no se instancie hasta que se llame por primera vez, es decir hasta que sea utilizado. En el ciclo de vida de una aplicación, los métodos y propiedades de una clase que componen existen, es decir se instancian en memoria y permanecen ahí desde la creación&#160; hasta que la destrucción de la clase.</p>
<p align="justify">Una clase <strong>Customer</strong> dónde el constructor carga una lista genérica de <strong>Order</strong> –inicializa un objecto <strong>List&lt;Order&gt;</strong> – éste permanece en memoria pese a que incluso es posible que ni siquiera lo necesitemos. Hasta <strong>.NET Framework 4.0</strong> la implantación de este tipo de inicializaciones se realiza mediante programación retrasando la inicialización de un objeto hasta que realmente se necesitara; en el caso de la clase <strong>Customer</strong>, por ejemplo, instanciaríamos el objeto<strong> List&lt;Order&gt;</strong> en un método <strong>LoadOrders()</strong> o <strong>GetOrders(),</strong> por ejemplo. En el caso de <strong>multithreading</strong>, la cosa se complica aún más y a veces el planteamiento o diseño de la clase no es suficiente.</p>
<p align="justify">Como novedad esta característica ya forma parte en .NET Framework 4.0 a través de <a href="http://msdn.microsoft.com/en-us/library/dd642331(VS.100).aspx" target="_blank">System.Lazy&lt;T&gt;</a><strong></strong> y aporta soporte <strong>Thread-Safety</strong> y una política de propagación de excepciones consistente para dar soporte al <strong>multithreading.</strong>&#160;</p>
<p align="justify">A partir de ahora nos olvidaremos de las técnicas anteriores y utilizaremos <strong><a href="http://msdn.microsoft.com/en-us/library/dd642331(VS.100).aspx" target="_blank">System.Lazy&lt;T&gt;</a></strong> para inicializaciones perezosas. Veamos un ejemplo: </p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span>[] args)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   3:</span>     Lazy&lt;<span style="color: #0000ff">string</span>&gt; cadena = <span style="color: #0000ff">new</span> Lazy&lt;<span style="color: #0000ff">string</span>&gt;(<span style="color: #0000ff">delegate</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   4:</span>                                                 {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   5:</span>                                                     <span style="color: #0000ff">return</span> <span style="color: #006080">&quot;Ejemplo Lazy&quot;</span>;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   6:</span>                                                 });</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   7:</span>&#160; </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   8:</span>     Console.WriteLine(<span style="color: #0000ff">string</span>.Format(<span style="color: #006080">&quot;isValueCreated: {0}&quot;</span>, cadena.IsValueCreated));</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   9:</span>     <span style="color: #008000">//podemos ejecutar cualquier acción y no se ejecutará hasta...</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  10:</span>     Console.WriteLine(<span style="color: #0000ff">string</span>.Format(<span style="color: #006080">&quot;Value calling: {0}&quot;</span>, cadena.Value));</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  11:</span>     <span style="color: #008000">//... que no llamemos al valor</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  12:</span>     Console.WriteLine(<span style="color: #0000ff">string</span>.Format(<span style="color: #006080">&quot;isValueCreated: {0}&quot;</span>, cadena.IsValueCreated));</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  13:</span>&#160; </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  14:</span>     Console.Read();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  15:</span> }</pre>
</p></div>
</div>
<p>El constructor de <a href="http://msdn.microsoft.com/en-us/library/dd642331(VS.100).aspx" target="_blank">System.Lazy&lt;T&gt;</a> soporta las siguientes sobrecargas:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> Lazy();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> Lazy(Func&lt;T&gt; valueFactory);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   3:</span> <span style="color: #0000ff">public</span> Lazy(<span style="color: #0000ff">bool</span> isThreadSafe);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   4:</span> <span style="color: #0000ff">public</span> Lazy(Func&lt;T&gt; valueFactory, <span style="color: #0000ff">bool</span> isThreadSafe);</pre>
</p></div>
</div>
<p>El parámetro <strong>isThreadSafe</strong> se establecerá a true cuando necesitemos utilizar el objeto de forma concurrente por multiples threads, con valor predeterminado a false.</p>
<p>El parámetro <strong>valueFactory</strong> es un delegado del tipo<strong> Func&lt;T&gt;</strong> y será el encargado de inicializar el valor del objeto <strong>Lazy</strong>. Un ejemplo, además del expuesto en el ejemplo anterior, podría ser:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Order {}</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   2:</span>&#160; </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   3:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> OrderDb</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   4:</span> {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> List&lt;Order&gt; GetOrdersBy(<span style="color: #0000ff">string</span> idCustomer)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   6:</span>     {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   7:</span>         <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> List&lt;Order&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   8:</span>                    {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   9:</span>                        <span style="color: #0000ff">new</span> Order(),</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  10:</span>                        <span style="color: #0000ff">new</span> Order(),</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  11:</span>                        <span style="color: #0000ff">new</span> Order()</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  12:</span>                    };</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  13:</span>     }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  14:</span> }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  15:</span>&#160; </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  16:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Customer</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  17:</span> {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  18:</span>     <span style="color: #0000ff">public</span> Lazy&lt;List&lt;Order&gt;&gt; _orders;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  19:</span>&#160; </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  20:</span>     <span style="color: #0000ff">public</span> Customer(<span style="color: #0000ff">string</span> id)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  21:</span>     {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  22:</span>         <span style="color: #008000">//inicialización Lazy</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  23:</span>         _orders =<span style="color: #0000ff">new</span> Lazy&lt;List&lt;Order&gt;&gt;(() =&gt;  OrderDb.GetOrdersBy(id) );</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  24:</span>     }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  25:</span> }</pre>
</p></div>
</div>
<p>Con el siguiente programa de consola:</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">class</span> Program</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span>[] args)</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   4:</span>     {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   5:</span>         Customer customer = <span style="color: #0000ff">new</span> Customer(<span style="color: #006080">&quot;10&quot;</span>);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   6:</span>&#160; </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   7:</span>         <span style="color: #008000">//pese a que customer se ha incializado la Lista de Orders aún no:</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   8:</span>         Console.WriteLine(<span style="color: #0000ff">string</span>.Format(<span style="color: #006080">&quot;isValueCreated: {0}&quot;</span>, customer._orders.IsValueCreated));</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   9:</span>         <span style="color: #008000">//Solicitamos las ordenes</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  10:</span>         Console.WriteLine(<span style="color: #0000ff">string</span>.Format(<span style="color: #006080">&quot;calling Value.Count: {0}&quot;</span>, customer._orders.Value.Count));</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  11:</span>         <span style="color: #008000">//Ahora si se han instanciado</span></pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  12:</span>         Console.WriteLine(<span style="color: #0000ff">string</span>.Format(<span style="color: #006080">&quot;isValueCreated: {0}&quot;</span>, customer._orders.IsValueCreated));</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  13:</span>&#160; </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  14:</span>         Console.Read();</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  15:</span>     }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">  16:</span> }</pre>
</p></div>
</div>
<p>Cuya salida será:</p>
<p><a href="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/01/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://jtorresd.brinkster.net/boresight/wp-content/uploads/2010/01/image_thumb.png" width="407" height="152" /></a></p>
<p>Es importante tener en mente este tipo de inicializaciones sobre todo en procesos de mucho consumo de recursos, por ejemplo objetos de la capa de datos que almacenan datos binarios – imagen, video,-&#160; y que muchas veces no necesitamos que consuman memoria si no se van a utilizar. Como este ejemplo, muchos más…</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://josemigueltorres.net/boresight/?p=351&amp;feed=comments-rss2" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://josemigueltorres.net/boresight/?p=351&amp;title=Inicializaci%C3%B3n+con+System.Lazy%3CT%3E+en+.NET+Framework+4.0" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://josemigueltorres.net/boresight/?p=351&amp;title=Inicializaci%C3%B3n+con+System.Lazy%3CT%3E+en+.NET+Framework+4.0" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://josemigueltorres.net/boresight/?p=351&amp;title=Inicializaci%C3%B3n+con+System.Lazy%3CT%3E+en+.NET+Framework+4.0&amp;desc=Una%20de%20las%20novedades%20de%20.NET%20Framework%204.0%20es%20la%20presentaci%C3%B3n%20de%20la%20clase%20System.Lazy%26lt%3BT%26gt%3B.%20En%20programaci%C3%B3n%2C%20una%20inicializaci%C3%B3n%20Lazy%20%28el%20t%C3%A9rmino%20Lazy%20significa%2C%20literalmente%2C%20gandul%20o%20retardado%29%20es%20un%20m%C3%A9todo%20de%20inicializaci%C3%B3n%20que%20permite%20que%20el%20objeto%20no%20se%20instancie%20hasta%20que%20se%20llame%20por" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://josemigueltorres.net/boresight/?p=351&amp;t=Inicializaci%C3%B3n+con+System.Lazy%3CT%3E+en+.NET+Framework+4.0" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://josemigueltorres.net/boresight/?p=351&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://josemigueltorres.net/boresight/?p=351&amp;title=Inicializaci%C3%B3n+con+System.Lazy%3CT%3E+en+.NET+Framework+4.0&amp;summary=Una%20de%20las%20novedades%20de%20.NET%20Framework%204.0%20es%20la%20presentaci%C3%B3n%20de%20la%20clase%20System.Lazy%26lt%3BT%26gt%3B.%20En%20programaci%C3%B3n%2C%20una%20inicializaci%C3%B3n%20Lazy%20%28el%20t%C3%A9rmino%20Lazy%20significa%2C%20literalmente%2C%20gandul%20o%20retardado%29%20es%20un%20m%C3%A9todo%20de%20inicializaci%C3%B3n%20que%20permite%20que%20el%20objeto%20no%20se%20instancie%20hasta%20que%20se%20llame%20por&amp;source=josemiguel.torres" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://josemigueltorres.net/boresight/?p=351&amp;bm_description=Inicializaci%C3%B3n+con+System.Lazy%3CT%3E+en+.NET+Framework+4.0&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://josemigueltorres.net/boresight/?p=351&amp;title=Inicializaci%C3%B3n+con+System.Lazy%3CT%3E+en+.NET+Framework+4.0" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://josemigueltorres.net/boresight/?p=351&amp;title=Inicializaci%C3%B3n+con+System.Lazy%3CT%3E+en+.NET+Framework+4.0" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://josemigueltorres.net/boresight/?p=351&amp;title=Inicializaci%C3%B3n+con+System.Lazy%3CT%3E+en+.NET+Framework+4.0" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://josemigueltorres.net/boresight/?p=351" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Inicializaci%C3%B3n+con+System.Lazy%3CT%3E+en+.NET+Framework+4.0+-+http://b2l.me/eawnd&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-yahoomail">
			<a href="http://compose.mail.yahoo.com/?Subject=Inicializaci%C3%B3n+con+System.Lazy%3CT%3E+en+.NET+Framework+4.0&amp;body=Link: http://josemigueltorres.net/boresight/?p=351 (sent via shareaholic)%0D%0A%0D%0A----%0D%0A Una%20de%20las%20novedades%20de%20.NET%20Framework%204.0%20es%20la%20presentaci%C3%B3n%20de%20la%20clase%20System.Lazy%26lt%3BT%26gt%3B.%20En%20programaci%C3%B3n%2C%20una%20inicializaci%C3%B3n%20Lazy%20%28el%20t%C3%A9rmino%20Lazy%20significa%2C%20literalmente%2C%20gandul%20o%20retardado%29%20es%20un%20m%C3%A9todo%20de%20inicializaci%C3%B3n%20que%20permite%20que%20el%20objeto%20no%20se%20instancie%20hasta%20que%20se%20llame%20por" rel="nofollow" class="external" title="Email this via Yahoo! Mail">Email this via Yahoo! Mail</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://josemigueltorres.net/boresight/?feed=rss2&amp;p=351</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://josemigueltorres.net/boresight/?p=351</feedburner:origLink></item>
		<item>
		<title>Ejemplo de Sync entre Oracle y SQL Server Compact</title>
		<link>http://feedproxy.google.com/~r/josemigueltorres/rXsu/~3/M2Qmj7l-Vcg/</link>
		<comments>http://josemigueltorres.net/boresight/?p=345#comments</comments>
		<pubDate>Tue, 05 Jan 2010 12:25:20 +0000</pubDate>
		<dc:creator>josemiguel.torres</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[SQL Server Compact]]></category>
		<category><![CDATA[Sync Services for ADO.NET]]></category>

		<guid isPermaLink="false">http://josemigueltorres.net/boresight/?p=345</guid>
		<description><![CDATA[Publicado en MSDN Code Gallery nuevo ejemplo de entorno de sincronización para orígenes de datos Oracle 10g o superior con SQL Server Compact en modo peer-to-peer. Lo más destacado de este ejemplo es el modo de creación del proveedor específico para Oracle a través de la clase DbSyncProvide. Peses a que el ejemplo utiliza SQL [...]]]></description>
			<content:encoded><![CDATA[<p>Publicado en <a title="MSDN Code Gallery" href="http://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=sync&amp;ReleaseId=3761" target="_blank"><strong>MSDN Code Gallery </strong></a>nuevo ejemplo de entorno de sincronización para orígenes de datos <strong>Oracle 10g </strong>o superior con SQL Server Compact en <strong>modo peer-to-peer</strong>. Lo más destacado de este ejemplo es el modo de creación <strong>del proveedor específico para Oracle </strong>a través de la clase <a href="http://msdn.microsoft.com/es-es/library/microsoft.synchronization.data.dbsyncprovider.aspx" target="_blank"><strong>DbSyncProvide</strong></a>. Peses a que el ejemplo utiliza SQL Server Compact, podemos hace uso tanto del nuevo proveedor específico para <a href="http://blogs.msdn.com/sync/archive/2009/10/19/sync-framework-2-0-available-for-download.aspx"><strong>SQL Server Express</strong></a>, incluido en MS Sync Framework 2.0, como SQL Server.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://josemigueltorres.net/boresight/?p=345&amp;feed=comments-rss2" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://josemigueltorres.net/boresight/?p=345&amp;title=Ejemplo+de+Sync+entre+Oracle+y+SQL+Server+Compact" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://josemigueltorres.net/boresight/?p=345&amp;title=Ejemplo+de+Sync+entre+Oracle+y+SQL+Server+Compact" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://josemigueltorres.net/boresight/?p=345&amp;title=Ejemplo+de+Sync+entre+Oracle+y+SQL+Server+Compact&amp;desc=Publicado%20en%20MSDN%20Code%20Gallery%20nuevo%20ejemplo%20de%20entorno%20de%20sincronizaci%C3%B3n%20para%20or%C3%ADgenes%20de%20datos%20Oracle%2010g%20o%20superior%20con%20SQL%20Server%20Compact%20en%20modo%20peer-to-peer.%20Lo%20m%C3%A1s%20destacado%20de%20este%20ejemplo%20es%20el%20modo%20de%20creaci%C3%B3n%20del%20proveedor%20espec%C3%ADfico%20para%20Oracle%20a%20trav%C3%A9s%20de%20la%20clase%20DbSyncProvide.%20P" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://josemigueltorres.net/boresight/?p=345&amp;t=Ejemplo+de+Sync+entre+Oracle+y+SQL+Server+Compact" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://josemigueltorres.net/boresight/?p=345&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://josemigueltorres.net/boresight/?p=345&amp;title=Ejemplo+de+Sync+entre+Oracle+y+SQL+Server+Compact&amp;summary=Publicado%20en%20MSDN%20Code%20Gallery%20nuevo%20ejemplo%20de%20entorno%20de%20sincronizaci%C3%B3n%20para%20or%C3%ADgenes%20de%20datos%20Oracle%2010g%20o%20superior%20con%20SQL%20Server%20Compact%20en%20modo%20peer-to-peer.%20Lo%20m%C3%A1s%20destacado%20de%20este%20ejemplo%20es%20el%20modo%20de%20creaci%C3%B3n%20del%20proveedor%20espec%C3%ADfico%20para%20Oracle%20a%20trav%C3%A9s%20de%20la%20clase%20DbSyncProvide.%20P&amp;source=josemiguel.torres" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://josemigueltorres.net/boresight/?p=345&amp;bm_description=Ejemplo+de+Sync+entre+Oracle+y+SQL+Server+Compact&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://josemigueltorres.net/boresight/?p=345&amp;title=Ejemplo+de+Sync+entre+Oracle+y+SQL+Server+Compact" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://josemigueltorres.net/boresight/?p=345&amp;title=Ejemplo+de+Sync+entre+Oracle+y+SQL+Server+Compact" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://josemigueltorres.net/boresight/?p=345&amp;title=Ejemplo+de+Sync+entre+Oracle+y+SQL+Server+Compact" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://josemigueltorres.net/boresight/?p=345" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Ejemplo+de+Sync+entre+Oracle+y+SQL+Server+Compact+-+http://b2l.me/czrtx&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-yahoomail">
			<a href="http://compose.mail.yahoo.com/?Subject=Ejemplo+de+Sync+entre+Oracle+y+SQL+Server+Compact&amp;body=Link: http://josemigueltorres.net/boresight/?p=345 (sent via shareaholic)%0D%0A%0D%0A----%0D%0A Publicado%20en%20MSDN%20Code%20Gallery%20nuevo%20ejemplo%20de%20entorno%20de%20sincronizaci%C3%B3n%20para%20or%C3%ADgenes%20de%20datos%20Oracle%2010g%20o%20superior%20con%20SQL%20Server%20Compact%20en%20modo%20peer-to-peer.%20Lo%20m%C3%A1s%20destacado%20de%20este%20ejemplo%20es%20el%20modo%20de%20creaci%C3%B3n%20del%20proveedor%20espec%C3%ADfico%20para%20Oracle%20a%20trav%C3%A9s%20de%20la%20clase%20DbSyncProvide.%20P" rel="nofollow" class="external" title="Email this via Yahoo! Mail">Email this via Yahoo! Mail</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://josemigueltorres.net/boresight/?feed=rss2&amp;p=345</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://josemigueltorres.net/boresight/?p=345</feedburner:origLink></item>
	</channel>
</rss>
