<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>.Net Extremo &#187; Genéricos</title>
	<atom:link href="http://acollado.wordpress.com/tag/genericos/feed/" rel="self" type="application/rss+xml" />
	<link>http://acollado.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Mon, 12 Oct 2009 19:56:43 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>es</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='acollado.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/284f0b0cc5215cefbd5947b3a2af8361?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>.Net Extremo &#187; Genéricos</title>
		<link>http://acollado.wordpress.com</link>
	</image>
			<item>
		<title>Delegados genéricos</title>
		<link>http://acollado.wordpress.com/2008/05/10/delegados-genericos/</link>
		<comments>http://acollado.wordpress.com/2008/05/10/delegados-genericos/#comments</comments>
		<pubDate>Sat, 10 May 2008 20:39:46 +0000</pubDate>
		<dc:creator>acollado</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Delegados]]></category>
		<category><![CDATA[Genéricos]]></category>

		<guid isPermaLink="false">http://acollado.wordpress.com/?p=27</guid>
		<description><![CDATA[Para poder entender correctamente los delegados genéricos debemos comprender antes algunos aspectos que explico a continuación.
Delegados:
Los delegados son una parte fundamental del Framework de .Net, pero… ¿Que es exactamente un delegado?, un delegado es un tipo que apunta a un método. Con la característica principal que dicho método ha de tener el mismo tipo de [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=acollado.wordpress.com&blog=3657593&post=27&subd=acollado&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p class="MsoNormal">Para poder entender correctamente los delegados genéricos debemos comprender antes algunos aspectos que explico a continuación.</p>
<h3><strong>Delegados:</strong></h3>
<p class="MsoNormal">Los delegados son una parte fundamental del Framework de .Net, pero… ¿Que es exactamente un delegado?, un delegado es un tipo que apunta a un método. Con la característica principal que dicho método ha de tener el mismo tipo de parámetros y valor devuelto.</p>
<table class="MsoNormalTable" style="border-collapse:collapse;height:29px;" border="0" cellspacing="0" cellpadding="0" width="682">
<tbody>
<tr>
<td style="background:#d6e3bc none repeat scroll 0;width:432.2pt;padding:0 5.4pt;" width="660" valign="top"><span style="color:#339966;"><strong><span>Nota:<br />
Los delegados son algo parecido a los punteros   a funciones de C++, pero más potentes<br />
y seguros.</span></strong></span></td>
</tr>
</tbody>
</table>
<p class="MsoNormal">
<p class="MsoNormal">
<p class="MsoNormal">Veamos un ejemplo:</p>
<p class="MsoNormal"><strong>C#</strong></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;">class</span><span style="font-size:10pt;"> <span style="color:#2b91af;">Program</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> {</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:green;">/*</span></span></p>
<p><span style="font-size:10pt;"><span style="color:green;"> Declaramos un delegado que apuntará a un metodo<br />
</span></span><span style="font-size:10pt;"><span style="color:green;">que ha de retornar un tipo int y tener dos parametros de tipo int</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"><span style="color:green;">*/</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">private</span> <span style="color:blue;">delegate</span> <span style="color:blue;">int</span> <span style="color:#2b91af;">CalculateHandler</span>(<span style="color:blue;">int</span> numA, <span style="color:blue;">int</span> numB);</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">static</span> <span style="color:blue;">void</span> Main(<span style="color:blue;">string</span>[] args)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">{</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:green;">//Creamos una instancia del delegado  que apunte al metodo Calc</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:#2b91af;">CalculateHandler</span> calcDelegate = <span style="color:blue;">new</span> <span style="color:#2b91af;">CalculateHandler</span>(Calc);</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:green;">//Invocamos al delegado </span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;Resultado de la invocación al delegado: &#8220;</span>+calcDelegate(5,5).ToString());</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:green;">//Otra forma de invocar al delegado</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;Resultado de la invocación al delegado con Invoke: &#8220;</span>+calcDelegate.Invoke(5, 5).ToString());</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:#2b91af;">Console</span>.Read();</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> }</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:green;">//Aquí hacemos la declaración del metodo para nuentro     delegado. </span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">private</span><span style="font-size:10pt;"> <span style="color:blue;">static</span> <span style="color:blue;">int</span> Calc(<span style="color:blue;">int</span> numA, <span style="color:blue;">int</span> numB)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">{</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">return</span> numA + numB;</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> }</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;"> }</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;"> </span></p>
<p class="MsoNormal"><strong><span style="font-size:10pt;line-height:115%;">VB</span></strong></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;">Module</span><span style="font-size:10pt;"> Module1</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p><span style="font-size:10pt;"><span style="color:green;">&#8216;Declaramos un delegado que apuntará a un metodo que ha de retornar un tipo Integer  y tener dos parametros de tipo Integer<br />
</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;">Delegate</span><span style="font-size:10pt;"> <span style="color:blue;">Function</span> CalculateHandler(<span style="color:blue;">ByVal</span> numA <span style="color:blue;">As</span> <span style="color:blue;">Integer</span>, <span style="color:blue;">ByVal</span> numB <span style="color:blue;">As</span> <span style="color:blue;">Integer</span>) <span style="color:blue;">As</span> <span style="color:blue;">Integer</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">Sub</span><span style="font-size:10pt;"> Main()</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:green;">&#8216;Creamos una instancia del delegado  que apunte al metodo Calc</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">Dim</span><span style="font-size:10pt;"> calcDelegate <span style="color:blue;">As</span> <span style="color:blue;">New</span> CalculateHandler(<span style="color:blue;">AddressOf</span> Calc)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;"><span style="color:green;">&#8216;Invocamos al delegado<br />
</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> Console.WriteLine(<span style="color:#a31515;">&#8220;Resultado de la invocación al delegado: &#8220;</span> + calcDelegate(5, 5).ToString())</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:green;">&#8216;Otra forma de invocar al delegado</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> Console.WriteLine(<span style="color:#a31515;">&#8220;Resultado de la invocación al delegado con Invoke: &#8220;</span> + calcDelegate.Invoke(5, 5).ToString())</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">Console.Read()</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">End</span> <span style="color:blue;">Sub</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">Private</span> <span style="color:blue;">Function</span> Calc(<span style="color:blue;">ByVal</span> numA <span style="color:blue;">As</span> <span style="color:blue;">Integer</span>, <span style="color:blue;">ByVal</span> numB <span style="color:blue;">As</span> <span style="color:blue;">Integer</span>)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">Return</span><span style="font-size:10pt;"> numA + numB</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">End</span> <span style="color:blue;">Function</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;">End</span><span style="font-size:10pt;line-height:115%;"> <span style="color:blue;">Module</span></span></p>
<p>En este sencillo ejemplo declaramos un delegado que apunta a una función y despues lo invocamos.</p>
<h3><strong>Tipos genéricos:</strong></h3>
<p>Desde la versión del Framework 2.0  se incluyen los tipos genéricos. Los tipos genéricos nos servirán para declarar código genérico, de forma que podremos preparar un elemento para recibir cualquier tipo.</p>
<table class="MsoNormalTable" style="border-collapse:collapse;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="background:#d6e3bc none repeat scroll 0;width:432.2pt;padding:0 5.4pt;" width="660" valign="top"><span style="color:#339966;"><strong>Nota:<br />
Antes de la implementación de los tipos genéricos se podía obtener un   resultado parecido con  el tipo object, que es el  tipo base del   cual descienden todos los tipos del .Net Framework.</strong></span></td>
</tr>
</tbody>
</table>
<p class="MsoNormal">
<p class="MsoNormal">Veamos un ejemplo:</p>
<p class="MsoNormal"><strong>C#</strong></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;">class</span><span style="font-size:10pt;"> <span style="color:#2b91af;">Program</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> {</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:green;">//Lista genérica que contendrá elementos del tipo empleado</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">static</span><span style="font-size:10pt;"> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Empleados</span>&gt; listaGenericaEmpleados;</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">static</span> <span style="color:blue;">void</span> Main(<span style="color:blue;">string</span>[] args)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">{</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> listaGenericaEmpleados = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Empleados</span>&gt;();</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> listaGenericaEmpleados.Add(<span style="color:blue;">new</span> <span style="color:#2b91af;">Empleados</span>());</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> }</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Empleados</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> {<span style="color:green;">//Miembros de la clase empleados. </span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> }</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;"> }</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;"> </span></p>
<p class="MsoNormal"><strong><span style="font-size:10pt;line-height:115%;">VB</span></strong></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;">Module</span><span style="font-size:10pt;"> Module1</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:green;">&#8216;Lista genérica que contendrá elementos del tipo empleado</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">Dim</span><span style="font-size:10pt;"> listaGenericaEmpleados <span style="color:blue;">As</span> List(<span style="color:blue;">Of</span> Empleados)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">Sub</span> Main()</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">Dim</span> listaGenericaEmpleados <span style="color:blue;">As</span> <span style="color:blue;">New</span> List(<span style="color:blue;">Of</span> Empleados)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> listaGenericaEmpleados.Add(<span style="color:blue;">New</span> Empleados())</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">End</span> <span style="color:blue;">Sub</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">Class</span><span style="font-size:10pt;"> Empleados</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:green;">&#8216;Miembros de la clase empleados. </span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">End</span> <span style="color:blue;">Class</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;">End</span><span style="font-size:10pt;"> <span style="color:blue;">Module</span></span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;"> </span></p>
<p>En este ejemplo declaramos una lista que contendrá elmentos del tipo empleados que ha sido definido por nosotros.</p>
<h3><strong>Delegados genénericos:</strong></h3>
<p>Al principio de este post, he descrito que un delegado ha de tener el mismo tipo de parámetros y valor devuelto que el método al que apuntará, pero…<strong> ¿Y si un delegado se pudiera declarar con parámetros del tipo genérico, y así poder dar flexibilidad a la función a la que apunte para poder implementar cualquier tipo de parámetros sin la necesidad de que sean del tipo object?</strong> Eso se consigue con los delegados genéricos.</p>
<p class="MsoNormal"><strong><span style="font-size:10pt;line-height:115%;">C#</span></strong></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">class</span><span style="font-size:10pt;"> <span style="color:#2b91af;">Program</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> {</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"><span style="color:green;">//Declaramos un delegado genérico</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">delegate</span> <span style="color:blue;">string</span> <span style="color:#2b91af;">GenericDelegate</span>&lt;T,T2&gt;(T valor1, T2 valor2);</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">static</span> <span style="color:blue;">void</span> Main(<span style="color:blue;">string</span>[] args)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> {</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"><span style="color:green;">//Instanciamos el delegado genérico con tipos int y string<br />
</span></span><span style="font-size:10pt;">GenericDelegate</span><span style="font-size:10pt;">&lt;<span style="color:blue;">int</span>, <span style="color:blue;">string</span>&gt; delegado1 = <span style="color:blue;">new</span> <span style="color:#2b91af;">GenericDelegate</span>&lt;<span style="color:blue;">int</span>, <span style="color:blue;">string</span>&gt;(Valores);</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:#2b91af;">Console</span>.WriteLine(delegado1(1,<span style="color:#a31515;">&#8220;1&#8243;</span>));</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;"><span style="color:green;">//Instanciamos el delegado genérico con tipos string y string</span></span><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;">GenericDelegate</span><span style="font-size:10pt;">&lt;<span style="color:blue;">string</span>, <span style="color:blue;">string</span>&gt; delegado2 = <span style="color:blue;">new</span> <span style="color:#2b91af;">GenericDelegate</span>&lt;<span style="color:blue;">string</span>, <span style="color:blue;">string</span>&gt;(Valores);</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">Console</span><span style="font-size:10pt;">.WriteLine(delegado2(<span style="color:#a31515;">&#8220;1&#8243;</span>, <span style="color:#a31515;">&#8220;1&#8243;</span>));</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:#2b91af;">Console</span>.Read();</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">}</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">string</span> Valores(<span style="color:blue;">int</span> val1, <span style="color:blue;">string</span> val2)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> {</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">return</span> val1 + val2;</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> }</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">string</span> Valores(<span style="color:blue;">string</span> val1, <span style="color:blue;">string</span> val2)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">{</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">return</span> val1 + val2;</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> }</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;"> }</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;"> </span></p>
<p class="MsoNormal"><strong><span style="font-size:10pt;line-height:115%;">VB</span></strong></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;">Module</span><span style="font-size:10pt;"> Module1</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:green;">&#8216;Declaramos el delegado genérico</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">Delegate</span><span style="font-size:10pt;"> <span style="color:blue;">Function</span> GenericDelegate(<span style="color:blue;">Of</span> T, T2)(<span style="color:blue;">ByVal</span> val1 <span style="color:blue;">As</span> T, <span style="color:blue;">ByVal</span> val2 <span style="color:blue;">As</span> T2) <span style="color:blue;">As</span> <span style="color:blue;">String</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">Sub</span><span style="font-size:10pt;"> Main()</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:green;">&#8216;Instanciamos el delegado genérico con tipos int y string</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">Dim</span><span style="font-size:10pt;"> delegado1 <span style="color:blue;">As</span> <span style="color:blue;">New</span> GenericDelegate(<span style="color:blue;">Of</span> <span style="color:blue;">Integer</span>, <span style="color:blue;">String</span>)(<span style="color:blue;">AddressOf</span> Valores)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">Console.WriteLine(delegado1(1, <span style="color:#a31515;">&#8220;1&#8243;</span>))</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:green;">&#8216;Instanciamos el delegado genérico con tipos string y string</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">Dim</span><span style="font-size:10pt;"> delegado2 <span style="color:blue;">As</span> <span style="color:blue;">New</span> GenericDelegate(<span style="color:blue;">Of</span> <span style="color:blue;">String</span>, <span style="color:blue;">String</span>)(<span style="color:blue;">AddressOf</span> Valores)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">Console.WriteLine(delegado2(<span style="color:#a31515;">&#8220;1&#8243;</span>, <span style="color:#a31515;">&#8220;1&#8243;</span>))</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> Console.Read()</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">End</span><span style="font-size:10pt;"> <span style="color:blue;">Sub</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">Private</span> <span style="color:blue;">Function</span> Valores(<span style="color:blue;">ByVal</span> val1 <span style="color:blue;">As</span> <span style="color:blue;">Integer</span>, <span style="color:blue;">ByVal</span> val2 <span style="color:blue;">As</span> <span style="color:blue;">String</span>)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">Return</span> val1 + val2</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">End</span> <span style="color:blue;">Function</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">Private</span> <span style="color:blue;">Function</span> Valores(<span style="color:blue;">ByVal</span> val1 <span style="color:blue;">As</span> <span style="color:blue;">String</span>, <span style="color:blue;">ByVal</span> val2 <span style="color:blue;">As</span> <span style="color:blue;">String</span>)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span><span style="font-size:10pt;">Return</span><span style="font-size:10pt;"> val1 + val2</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> <span style="color:blue;">End</span> <span style="color:blue;">Function</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;">End</span><span style="font-size:10pt;line-height:115%;"> <span style="color:blue;">Module</span></span></p>
<p class="MsoNormal"><strong><span style="font-size:10pt;line-height:115%;"> </span></strong></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;"> </span></p>
<p>Como hemos podido ver, los delegados genericos nos ofrecen una flexibilidad importante a la hora de desarrollar de una forma más escalable.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/acollado.wordpress.com/27/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/acollado.wordpress.com/27/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/acollado.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/acollado.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/acollado.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/acollado.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/acollado.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/acollado.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/acollado.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/acollado.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/acollado.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/acollado.wordpress.com/27/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=acollado.wordpress.com&blog=3657593&post=27&subd=acollado&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://acollado.wordpress.com/2008/05/10/delegados-genericos/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f2870ea16cef0e78fd4be56eaadcc37e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">acollado</media:title>
		</media:content>
	</item>
	</channel>
</rss>