event method c# code example
Example 1: eventos c#
public class EventoArgs
{
public string Texto {get;}
public EventoArgs(string texto)
{
this.Texto = texto;
}
}
public delegate void ManejadorEventosEjemplo(object sender, EventoArgs e)
public class ClasePrueba
{
public event ManejadorEventosEjemplo EventoEjemplo;
protected virtual void LlamarEvento()
{
EventoEjemplo?.Invoke(this, new EventoArgs("Hola"));
}
}
Example 2: c# events
class Publisher
{
public delegate void MyEventHandler (object source, EventArgs args);
public event MyEventHandler myEvents;
protected virtual void Method1()
{
if(myEvents != null)
{
myEvents(this, EventArgs.Empty);
}
}
public void Method2()
{
Console.WriteLine("This method is called from the Main function...");
Method1();
}
}
public class Subscriber
{
public void Method1(object source, EventArgs e)
{
Console.WriteLine("The work done by the subscriber after the event was raised...");
}
}
class Program
{
static void Main(string[] args)
{
var myPublisher = new Publisher();
var mySubscriber = new Subscriber();
myPublisher.myEvents += mySubscriber.Method1;
myPublisher.Method2();
}
}
public class MyNewEventArgs : EventArgs
{
public string newData {get; set;}
}
public delegate void EventHandler (object source, MyNewEventArgs args);
myEvents(this, new MyNewEventArgs() { newData = "Hello World!"} );
public void Method1(object source, MyNewEventArgs e)