Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Dotnet
- {
- public class Dog
- {
- private string _name;
- public string Name
- {
- get { return _name; }
- set { _name = value; }
- }
- public event EventHandler HasSpoken;
- public void Speak(string what = "bark")
- {
- if(HasSpoken != null) // se tiene que checkear si hay algun metodo que "escuche" a evento, sino tira error
- HasSpoken(this, EventArgs.Empty); // en caso de que halla alguien suscripto , si lo levanto
- }
- }
- }
- namespace Dotnet
- {
- public class Program
- {
- public static void Main(string[] args)
- {
- Trainer t1 = new Trainer();
- Dog d1 = new Dog();
- d1.Speak();
- }
- }
- }
- namespace Dotnet
- {
- public class Trainer
- {
- void Operate()
- {
- var dog = new Dog();
- dog.HasSpoken += dog_HasSpoken; // suscripcion al event
- }
- void dog_HasSpoken(object sender, EventArgs args)
- {
- //cuando el dog llama a hasspoken se ejecuta este evento y cualquier otro que este suscrito al evento de la manra +=
- Console.WriteLine("Shut up");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement