node-clr icon indicating copy to clipboard operation
node-clr copied to clipboard

Events and delegate types

Open jochemstoel opened this issue 6 years ago • 1 comments

Can we use delegates like below, is there an equivalent of this using CLR? If so, how would I go about doing that?

using System;

namespace SampleApp {
   public delegate string MyDel(string str);
	
   class EventProgram {
      event MyDel MyEvent;
		
      public EventProgram() {
         this.MyEvent += new MyDel(this.WelcomeUser);
      }
		
      public string WelcomeUser(string username) {
         return "Welcome " + username;
      }
		
      static void Main(string[] args) {
         EventProgram obj1 = new EventProgram();
         string result = obj1.MyEvent("AtsushiSuzuki");
         Console.WriteLine(result);
      }

   }
}

jochemstoel avatar Sep 05 '18 15:09 jochemstoel

You can consume .NET events by event.add(callback) and event.remove(callback). You cannnot define .NET events (this is by design), or invoke an event (not implemented).

AtsushiSuzuki avatar Sep 05 '18 16:09 AtsushiSuzuki