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

  • Home
  • SEARCH
  • 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 6651595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:01:51+00:00 2026-05-26T01:01:51+00:00

Is there a reason to use this: bool flag; public Form1() { if (flag)

  • 0

Is there a reason to use this:

bool flag;

public Form1()
{
    if (flag) byDelegate = square;
    else byDelegate = cube;
    Text = byDelegate(3).ToString();          
}

int square(int i) { return i * i; }
int cube(int i) { return i * i * i; }

delegate int delegate1(int x);
delegate1 byDelegate;

Rather than:

bool flag;

public Form2()
{
    Text = fakeDelegate(3).ToString(); 
}

int square(int i) { return i * i; }
int cube(int i) { return i * i * i; }

int fakeDelegate(int i)
{
    if (flag) return square(i);
    else return cube(i);
}

Thanks.

  • 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-26T01:01:52+00:00Added an answer on May 26, 2026 at 1:01 am

    Delegates are usually used asynchronously, for events or passed into methods/types so that the method the delegate points to (‘function pointers’) can be called later. In your case there looks like there’s no advantage as you’re doing everything synchronously.

    For example

    private Action<double> _performWhenFinished.
    
    public Form1(Action<double> performWhenFinished)
    {
        _performWhenFinished = performWhenFinished;        
    }
    
    public void CalculatePi()
    {
       double pie = 0d;
        // Create a new thread, take 2 minutes to perform the task
    
        // Thread.Wait etc., then and run your delegate 
        _performWhenFinished(pie);
    }
    

    You generally don’t need to declare your own delegates in 3.5 upwards unless you want to your code to provide a bit more meaning via those declarations. The Action type and the Func type (func provides a method with a return value) save you the effort.

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

Sidebar

Related Questions

Is there ever a reason to use Type parameters over generics, ie: // this...
Is there any reason to do use block initialization, like this: x = Observer.new
Is there any reason to use a varchar field instead of a date field
Is there a reason to use a single incrementing field for a primary key
Is there any reason to use Spring MVC (or other similar frameworks) as a
Is there any reason to use Dynamic Data Exchange (DDE) in a modern Windows
Pretty self explanatory question. Is there any reason to use one or the other?
Is there any reason not to use the bitwise operators &, |, and ^
Is there any reason for the use of 'T' in generics? Is it some
Is there any particular reason to use one over the other? I personally tend

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.