Created
November 24, 2019 03:33
-
-
Save diegomichell/7b5b219c1998d774cacb8506ed714b48 to your computer and use it in GitHub Desktop.
C# Delegates example
This file contains hidden or 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; | |
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