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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:40:24+00:00 2026-05-16T20:40:24+00:00

First, I was reading some forums and the help in MSDN and all says

  • 0

First, I was reading some forums and the help in MSDN and all says that a delegate can’t be overloaded.

Now, I want to have something like this:

public delegate void OneDelegate();
public delegate void OneDelegate(params object[] a);

public void DoNothing(params object[] a) {}
public void DoSomething() { /* do something */ }

private OneDelegate someFunction;

someFunction = new OneDelegate(DoSomething);
someFunction = new OneDelegate(DoNothing);

So, like you know, you CAN’T do this, because OneDelegate only refers to the first one and not the second one. But, is there a way for doing this? or something like that?

PS1: I want to have any number of OneDelegate declarations, not just one or two.

  • 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-16T20:40:24+00:00Added an answer on May 16, 2026 at 8:40 pm

    Imagine for a moment this was possible. Suppose I could have an overloaded delegate:

    public delegate void OneDelegate(int i);
    public delegate void OneDelegate(string s);
    

    Now imagine I declare a variable of this type and then assign a function to it, for example:

    OneDelegate myDelegate = StringMethod;
    

    where StringMethod is declared thusly:

    public void StringMethod(string s) { Console.WriteLine(s); }
    

    Now you pass myDelegate to some other code, and that code does this:

    myDelegate(47);
    

    What do you expect to happen in this case? How can the runtime call StringMethod() with an integer argument?

    If you really want a delegate that can take any set of parameters at all, then the only option is to have one with a params object[] array:

    public delegate void OneDelegate(params object[] parameters);
    

    But then you will have to assign to it a function that can actually handle any object array, for example:

    public void MyMethod(params object[] parameters)
    {
        if (parameters == null || parameters.Length == 0)
            throw new ArgumentException("No parameters specified.");
        if (parameters.Length > 1)
            throw new ArgumentException("Too many parameters specified.");
    
        if (parameters[0] is int)
            IntMethod((int) parameters[0]);
        else if (parameters[0] is string)
            StringMethod((string) parameters[0]);
        else
            throw new ArgumentException("Unsupported parameter type.");
    }
    

    As you can see, this gets messy real quick. Therefore, I submit to you that if you need such a delegate, you have probably made a mistake somewhere in your architectural design. Identify this flaw and fix the design before you proceed with the implementation, as otherwise the maintainability of your code will suffer.

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

Sidebar

Related Questions

I'm reading input in a C++ program. First some integers, then a string. When
First of all, I know how to build a Java application. But I have
I have been reading this post on the codeigniter forum, http://codeigniter.com/forums/viewthread/174928/ and it has
I have a django form that first validates its data through calling form.is_valid(). If
After reading the Head First Design Patterns book and using a number of other
I'm reading through head first design patterns at the moment and while the book
Below are two ways of reading in the commandline parameters. The first is the
First off, I am using Windows XP. I have multiple hard drives and it
i would like to implement some kind of service my customers can use to
i had these questions in my mind since i was reading some new topics

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.