Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6959191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:14:20+00:00 2026-05-27T15:14:20+00:00

During one of my lab sessions I was given a problem related to delegates

  • 0

During one of my lab sessions I was given 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 time inside the methods. But one of the Lab Faculty suggested me to do this either:

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 to move the conditions out from the methods into the Main() method. The End result is the same but I am still confused about which way is the better way to do it especially considering the use of delegates in large programs, whether the conditional check should be moved into the methods or should be kept outside.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:14:21+00:00Added an answer on May 27, 2026 at 3:14 pm

    Neither of them is good code. These are just constructed samples to show the syntax and usage of delegates. In this sample you wouldn’t need a delegate at all, because it is just a normal call to a function.

    Delegates are useful under the following conditions:

    • Methods determinded dynamically at run time (method A, B or C is called)
    • Callback methods (enable to specify a method which is called by some other method)
    • Multicast operations (method A, B and C are called by some code X. X does not have any information about A, B or C)

    The two samples don’t have the same meaning. I would prefer the first version because it does what it says. GreetGoodMorning checks whether it’s morning or not and doesn’t do anything when it isn’t. I would rewrite the 2nd version into the following:

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

    Nobody should write any code just to use a delegate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two textures that are both .jpg, which represent a sky (one during
Im facing one problem in streaming data capture for reading the broadcast data during
I need to access a file larger than 2gb using C. During one run
I need to change session during one cucumber scenario. For example Given session name
I saw this post: Typos… Just use option strict and explicit please.. during one
For some reason during loading one of the scenes in my game, the background
I would like to be able to merge two files into one during configure
For data validation, for example during migrations from one XML library to a new
I am new to android. I got one issue during run my first hello
During Session Start, one has access to the Request object. How about Session End,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.