//Atribuo um método criado em runtime para o delegate de string e de void parametro inteiro public static void TestarLambdaDelegate() { DelegateString delegString = () => "Teste Lambda String"; Console.WriteLine(delegString()); DelegateVoidParametro delegVoid1 = (x) => Console.WriteLine(x * 10); Console.WriteLine("Digite um numero para multiplicar por 10"); delegVoid1(Convert.ToInt32(Console.ReadLine())); //Para uma variável fora da expressão, o compilador cria uma variável para o escopo da expressão, contendo o mesmo valor que foi setado antes de inicar o método e ela o mesmo tempo de vida do método bool teste = false; DelegateVoidParametro delegVoid2 = (x) => { teste = true; for (int i = 0; i <= x; i++) { Thread.Sleep(500); Console.WriteLine(i); } }; Console.WriteLine("Digite um numero para ver contando até ele"); delegVoid2(Convert.ToInt32(Console.ReadLine())); if (teste) { Console.WriteLine("bool true"); } else { Console.WriteLine("bool false"); } }
static void Main(string[] args) { //Ask for input Console.WriteLine("Enter a number(x): "); String xStr = Console.ReadLine(); int x = Convert.ToInt32(xStr); Console.WriteLine("Enter a second number(y):"); String yStr = Console.ReadLine(); int y = Convert.ToInt32(yStr); //Create a DelegateInt instance to pass the AddFunction DelegateInt del = AddFunction; //Call the method that passes a delegate, referencing the addfunction method AddCallerMethod(x, y, del); //Ask for more input Console.WriteLine("\n\nNow give me a sentence."); string sentenceOne = Console.ReadLine(); Console.WriteLine("And a second sentence."); string sentenceTwo = Console.ReadLine(); //Create DelegateString instance DelegateString del2 = ConcatenateFunction; ConcatenateCallerMethod(sentenceOne, sentenceTwo, del2); Console.ReadLine(); }
// Use this for initialization void Start() { e = new Event(); eS = new EventString(); e.Listen(NormalEvent); eS.Listen(StringEvent); //Events need function call to invoke e.Trigger(); eS.Trigger("EVENT RIGGERED!"); //Delegates del += NormalEvent; delString += StringEvent; delReturnInt += EventReturnValue; //Delegates can directly call itself as a function for invocation del(); delString("DELEGATE TRIGGERED!"); Debug.Log("EVENT RETURN VALUE " + delReturnInt()); action += NormalEvent; actionString += StringEvent; //Action can directly call itself as a function for invocation action(); actionString("ACTION TRIGGERED!"); }
public static void TestarMultipleDelegate() { Console.WriteLine("Multiple Delegate"); //+= para atribuir mais metodos domesmo tipo e com mesmos parametros ao delegate DelegateString deleg = MetodoString1; deleg += MetodoString2; //Percorro lista de metodos e chamo um por vez foreach (var invocation in deleg.GetInvocationList()) { var teste = invocation.DynamicInvoke(); Console.WriteLine(teste); } }
public Converter(MainForm f, int wid, int hei, byte mi, string inp, string outp, string binp, byte tsk_mapstatics, byte tsk_season, bool tsk_uop, bool tsk_dds) { form = f; width = wid; height = hei; mapIndex = mi; inPath = inp; outPath = outp; binPath = binp; tileMatrix = new TileMatrix(inPath, mapIndex, mapIndex, width, height); task_mapstatics = tsk_mapstatics; task_season = tsk_season; task_uop = tsk_uop; task_dds = tsk_dds; CDAppendToLog = new DelegateString(form.AppendToLog); CDItemsState = new DelegateBool(form.SetItemsState); CDProgressBarSetMarquee = new DelegateBool(form.ProgressbarSetMarquee); CDProgressBarIncrease = new DelegateVoid(form.ProgressbarIncrease); CDProgressBarSetMax = new DelegateInt(form.ProgressbarSetMax); CDProgressBarSetVal = new DelegateInt(form.ProgressbarSetVal); CDSuccess = new DelegateVoid(form.Success); Log("Loading hues.mul into memory... ", true); Hues.Init(); Log("Done!\n", true); if (task_season != 0) { LoadLTDictionary(); } if (tsk_dds) { loadRadarcol(); bmp = new Bitmap(width, height, PixelFormat.Format16bppRgb555); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); bmpData = bmp.LockBits(rect, ImageLockMode.WriteOnly, bmp.PixelFormat); unsafe { bmpPtr = (byte *)bmpData.Scan0.ToPointer(); } } }
//Create a caller method for the concatenate function public static void ConcatenateCallerMethod(string a, string b, DelegateString delString) { Console.WriteLine("\n\nCombined that says: " + delString(a, b) + ".\n\n"); }