What is the correct way to use a delegate?

During one of my lab sessions, I was asked a problem related to delegates, which I solved as belows:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DelegateApp { delegate void GreeterDelegate(); class Program { static void GreetGoodMorning() { if (DateTime.Now.ToString().EndsWith("AM")) Console.WriteLine("Good Morning!"); } static void GreetGoodEvening() { if (DateTime.Now.ToString().EndsWith("PM")) Console.WriteLine("Good Evening!"); } static void Main(string[] args) { GreeterDelegate Greeters = new GreeterDelegate(GreetGoodMorning); Greeters += GreetGoodEvening; Greeters(); Console.ReadLine(); } } } 

What I did was that I used the conditions to check the time inside the methods. But one of the laboratory assistants suggested that I do this:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DelegateApp { delegate void GreeterDelegate(); class Program { static void GreetGoodMorning() { Console.WriteLine("Good Morning!"); } static void GreetGoodEvening() { Console.WriteLine("Good Evening!"); } static void Main(string[] args) { GreeterDelegate Greeters; if (DateTime.Now.ToString().EndsWith("AM")) Greeters = new GreeterDelegate(GreetGoodMorning); else Greeters = new GreeterDelegate(GreetGoodEvening); Greeters(); Console.ReadLine(); } } } 

He suggested translating conditions from methods into the Main() method. The end result is the same, but I'm still confused how this is the best way to do this, especially considering the use of delegates in large programs, whether conditional checking should be translated into methods or contained outside.

+4
source share
3 answers

None of them are good code. These are just created samples to show the syntax and usage of delegates. In this example, you don’t need a delegate at all, because this is a normal function call.

Delegates are useful under the following conditions:

  • Methods defined dynamically at run time (method A, B, or C is called)
  • Callback methods (allow you to specify a method that is called by some other method)
  • Multicast operations (method A, B, and C is called by some code X. X has no information about A, B, or C)

Two samples do not have the same value. I would prefer the first version because she does what she says. GreetGoodMorning checks whether it is morning or not and does nothing when it is not. I would rewrite the second version as follows:

  static void Greet() { if (DateTime.Now.ToString().EndsWith("PM")) Console.WriteLine("Good Evening!"); else Console.WriteLine("Good Morning!"); } 

No one should write code just to use the delegate.

+1
source

The latter sample is better because you do not have redundant checks that use the + fewer calls method. This will improve performance because validation will be performed at runtime.

+3
source

This is not a delegate question, but a coding practice.

In your code, you call both delegates, but only one displays the text in the console.

In the proposed code, only one delegate is called.

The second implementation is more efficient and the one you should use. From a programming point of view, the purpose of the code is also clear.

Whenever you are about to perform an action, you better decide which action you want to perform first, and then do it, rather than placing checks in each action.

If you like beans for dinner, you cannot pull each one out of the cupboard and then decide if you want beans and whether it can have beans in it or not. You check each one of them to see if it has beans and only get it.

+2
source

Source: https://habr.com/ru/post/1386085/


All Articles