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 6997233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:15:28+00:00 2026-05-27T20:15:28+00:00

Sometimes I encounter cases where I have to attach a method to a delegate

  • 0

Sometimes I encounter cases where I have to attach a method to a delegate but the signature doesn’t match, like trying to attach abc down there to somedelegate with the string parameter being “hi”.

public class test
{
   //...
   public void abc(int i, string x)
   {
      //Do Something
   }
   //...
}
public class test2
{
   somedelegate x;
   //...
   public test2()
   {
      //Do Something
      test y = new test();
      x += y.abc(,"hi");
   }
   delegate void somedelegate(int i);
}

I can work it around by creating another delegate with the correct signature then attaching it but it seems so unnecessarily complex. Can you do something like this in C#? Thanks.

EDIT: I guess there closest to what I wanted to achieve is:

x += (int i) => abc(i, "hi");
  • 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-27T20:15:28+00:00Added an answer on May 27, 2026 at 8:15 pm

    Yes, you can do this with closures

    [there’s a nice treatment of the subject on msdn, but like anything else in there it’s hard to find]

    The Big Picture

    • Write a method that can take all the parameters you need
    • Inside that method you return an anonymous method with the delegate-target signature it requires
    • This method’s call is itself the parameter in the delegate instantiation

    Yes, this is a bit Matrix-y. But way cool.

    delegate void somedelegate (int i);
    
    protected somedelegate DelegateSignatureAdapter ( string b, bool yesOrNo, ...) {
        // the parameters are local to this method, so we'll go w/ that.
    
        // our target delegate requires a single int parameter and void return
        return  (int a) => {
                    // your custom code here
                    // all calling arguements are in scope - use them as needed
    
        };  // don't forget the semicolon!
    }
    
    // our delegate call
    somedelegate myWarpedDelegate = new somedelegate (DelegateSignatureAdapter("someString", true));
    myWarpedDelegate (2543);
    myWarpedDelegate(15);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have not had much experience with VBA, but I sometimes use it at
Sometimes I encounter code that reads TSC with rdtsc instruction, but calls cpuid right
Sometimes it is weird to encounter File not Found Error in running. But we
I have this multi-dimensional array: char marr[][3] = {{abc},{def}}; Now if we encounter the
I rarely use inheritance, but when I do I sometimes encounter the following problem.
Sometimes I encounter this situation in Java while trying to use immutable objects. In
When reviewing, I sometimes encounter this kind of loop: i = begin while (
Sometimes I have to work on code that moves the computer clock forward. In
I have a problem. Sometimes when I close my Java applet using the Close
I have the following problem with GKSessions: Sometimes there is a huge delay when

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.