22 May 2019

c#basics event EventHandler OnEvent

delev

We learned about the event keyword and the EventHandler class. we learned the delegate which is defined for the event “behind the scenes” (object sender, EventArgs e). we learned about the EventArgs and saw how to create our own customized EventArgs (inherit from EventArgs).  we saw the naming convention of the event-name ,which should state the scenario in the correct time tense (i.e. VideoEncod-ed, VideoEncod-ing, etc.) . we saw that the Event is fired from a method call On<Event-Name> (in the publisher side); in the subscriber side- we saw that we have to define a method called <Event-name>EventHandler and to register it to the event. we saw that events are different from delegate, for example you cannot override the event using the “=”  operation, but instead- you must use the “+=” operation to add your method. then we saw how WinForm OnClick is registering the event and later on executing it. finally we saw how to register our method into the .NET Timer class and execute it in each interval

More topics covered:

  • delegate Combine
  • using new keyword on delegate
  • delegate BeginInvoke EndInvoke
  • Generic EventHandler<T>
  • Who is the objectsender
  • using this for the object sender

Links:

19 May 2019

c#basics delegate Action Func Lambda-Expression

delev

We learned about the event driven approach and the need of a contract to define a method signature. The .NET message signature type is called delegate and it helps us to store 1 or more methods with the same (exact) signature. Why? so we can Invoke these methods (stored in the delegate) whenever we want to (usually when an event occurs, for example: button-Click). We saw the Action<T> type and the Func<T> type which are actually a delegate wrappers. we understood the loosely couple concept in which we can take two non-related classes and bind them together using the delegate (and unbind them). Finally we saw how to create a delegate method using Lambda expression

More topics covered:

  • Publish-Subscriber
  • Creating Property of a delegate member
  • Using the delegate type as a method parameter
  • Using the delegate type as a method return value
  • Executing delegate using Invoke and without Invoke
  • Delegate can actually store 1 or more methods
  • Invoking a delegate execute all of the stored methods

Links:

Design a site like this with WordPress.com
Get started