Skip to content

Instantly share code, notes, and snippets.

@diegomichell
Created November 24, 2019 03:33
Show Gist options
  • Save diegomichell/7b5b219c1998d774cacb8506ed714b48 to your computer and use it in GitHub Desktop.
Save diegomichell/7b5b219c1998d774cacb8506ed714b48 to your computer and use it in GitHub Desktop.
C# Delegates example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
public delegate void OnCloseEventHandler(Modal modal);
public class Modal {
public string Title { get; set; }
protected List<OnCloseEventHandler> handlers = new List<OnCloseEventHandler>();
public void addOnCloseEventListener(OnCloseEventHandler handler) {
this.handlers.Add(handler);
}
public void removeOnCloseEventListener(OnCloseEventHandler handler)
{
this.handlers.Remove(handler);
}
public void Close() {
foreach (var handler in this.handlers)
{
handler(this);
}
}
}
class Program
{
public static void MostrarTitulo (Modal modal)
{
Console.WriteLine(modal.Title + " No anonimo");
}
static void Main(string[] args)
{
var modal = new Modal { Title = "Ventana Principal" };
OnCloseEventHandler onCloseEventHandler = (target) =>
{
Console.WriteLine(target.Title);
};
modal.addOnCloseEventListener(onCloseEventHandler);
modal.addOnCloseEventListener(MostrarTitulo);
modal.Close();
modal.removeOnCloseEventListener(onCloseEventHandler);
modal.Close();
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment