Last active
December 11, 2015 11:18
-
-
Save jpolvora/4592858 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Delegates | |
{ | |
public delegate void VoidSemParametros(); | |
public delegate void VoidComParametros(string valor); | |
public delegate int IntSemParametros(); | |
public delegate int IntComParametros(int x, int y); | |
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var programa = new Program(); | |
#region Instanciando Delegates | |
/* Instancia VoidSemParametros utilizando construtor (new()) */ | |
var delegateNew = new VoidSemParametros(programa.MetodoSemParametros); | |
delegateNew(); /* executa */ | |
/* Instancia VoidSemParametros apontando direto para um método com assinatura compatível */ | |
VoidSemParametros delegateExplicito = programa.OutroMetodoSemParametros; | |
delegateExplicito(); /* executa */ | |
/* Instancia VoidSemParametros utilizando anonymous delegate */ | |
VoidSemParametros delegateInline = delegate() { | |
Console.WriteLine("Anonymous delegate..."); | |
}; | |
/* Instancia um delegate combinando vários delegates */ | |
var delegateCombinado = delegateInline + delegateNew; | |
/* Concatena delegates */ | |
delegateCombinado += delegateExplicito; | |
/* O método Console.WriteLine possui várias sobrecargas, uma delas possui assinatura compatível com VoidSemParametros */ | |
delegateCombinado += Console.WriteLine; | |
/*executa todos os delegates que foram combinados na mesma ordem*/ | |
delegateCombinado(); | |
#endregion | |
#region VoidComParametros | |
var escreve = new VoidComParametros(programa.Escreve); | |
escreve("Escrito via método programa.Escreve"); | |
/* O método Console.WriteLine possui várias sobrecargas, uma delas possui assinatura compatível com VoidComParametros */ | |
VoidComParametros escreveDireto = Console.WriteLine; | |
escreveDireto("Escrito via Console.WriteLine"); | |
#endregion | |
#region IntSemParametros | |
var funcao = new IntSemParametros(programa.FuncaoQueRetornaInt); | |
escreve(funcao().ToString()); | |
#endregion | |
#region IntComParametros | |
var calculadora = new IntComParametros(programa.Soma); | |
var resultadoSoma = calculadora(3,3); | |
Console.WriteLine("Soma: {0}", resultadoSoma); | |
calculadora = programa.Multiplica; //mudou a referência para outro método | |
var resultadoMultiplicacao = calculadora(3,3); | |
Console.WriteLine("Multiplicação: {0}", resultadoMultiplicacao); | |
#endregion | |
#region Actions | |
/* Action é um delegate semelhante ao VoidSemParametros */ | |
var action = new Action(programa.MetodoSemParametros); | |
Console.WriteLine(action.Method.Name); | |
/* Action<T> é um delegate semelhante ao VoidComParametros se T for do tipo String */ | |
Action<string> actEscreve = programa.Escreve; | |
actEscreve("Escrito via Action<string>"); | |
#endregion | |
#region Func | |
Func<int> funqInt = programa.FuncaoQueRetornaInt; | |
Console.WriteLine("Func<int>: {0}", funqInt()); | |
//var funqLambda = new Func<int>(programa.FuncaoQueRetornaInt); | |
Func<int> funqLambda = () => { return programa.FuncaoQueRetornaInt(); }; | |
Console.WriteLine("Lambda: {0}", funqLambda()); | |
Func<int, int, int> funqParameters = programa.Soma; | |
Console.WriteLine("funqParameters: {0}", funqParameters(2, 3)); | |
funqParameters = programa.Multiplica; | |
Console.WriteLine("funqParameters: {0}", funqParameters(2, 3)); | |
#endregion | |
Console.ReadKey(); | |
} | |
public void MetodoSemParametros() { | |
Console.WriteLine("MetodoSemParametros"); | |
} | |
public void OutroMetodoSemParametros() { | |
Console.WriteLine("OutroMetodoSemParametros"); | |
} | |
public void Escreve(string valor) { | |
Console.WriteLine("Valor: {0}", valor); | |
} | |
public int Soma(params int[] args) { | |
int result = 0; | |
foreach (var arg in args) { | |
result = result + arg; | |
} | |
return result; | |
} | |
public int Soma(int x, int y) { | |
return x + y; | |
} | |
public int Multiplica(int x, int y){ | |
return x * y; | |
} | |
public int FuncaoQueRetornaInt(){ | |
return 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment