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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:46:33+00:00 2026-05-23T21:46:33+00:00

I have a static function in a class with two overloads. Both the overloads

  • 0

I have a static function in a class with two overloads. Both the overloads are quite the same with the exception of one or two parameters. string body is the only necessary parameter in my function which you can see, rest are optional parameters. But parameters object y and int x shouldn’t come together. So i had to write two overloads as below. I provide a sample code:

    public static void Foo(string body, string caption = "", int x = 0)
    {
        //leave it to me
    }

    public static void Foo(string body, string caption = "", object y = null)
    {
        //leave it to me
    }

Now when I want to call this static function from other classes, since string body is the only required parameter, I try to write at times:

ClassABC.Foo("hi there");

Which gives me this: The call is ambiguous between the following methods or properties. I know why this happens, and what ideally the solution is. But I need to know if anything else could be done in C# to tackle this.

Obviously, the compiler is confused as to choose which function to go for, but I wouldnt mind the compiler going for any considering both are the same without int x and object y. Three questions basically:

  1. Is there anyway to tell the compiler “take any” (almost impossible task, but still let me know)?

  2. If not, is there anyway I can create a single function to handle both cases? Something like this:

    public static void Foo(string body, string caption = "", int x = 0 || object y = null) // the user should be able to pass only either of them!
    {
        //again, I can handle this no matter what
    }
    
  3. Any other workarounds to solve this?

EDIT:

  1. I can not rename the two functions.

  2. I can not create more overloads. It’s not just these combinations possible. I should be able to write Foo(string body, int x) etc. So goes. Its virtually impossible to handle all conditions if parameters are more than say, 10!. In short, optional parameters are a must.

  • 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-23T21:46:33+00:00Added an answer on May 23, 2026 at 9:46 pm

    Add a separate overload to handle the cases with one or two arguments.

    public static void Foo(string body, string caption = "")
    {
    } 
    
    public static void Foo(string body, string caption, int x)
    {
    }
    
    public static void Foo(string body, string caption, object y)
    {
    }
    

    If you need to handle arbitrary combinations of arguments, then perhaps it’s time to group your optional arguments into a class of their own:

    public sealed class Options
    {
        public string Caption { get; set; }
        public int X { get; set; }
        public object Y { get; set; }
    }
    
    public static void Foo(string body, Options options)
    {
    
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK I have a static class that has two static members, a string and
I have a class called User with static function loginRequired(), which returns false if
I have two Java class files: primegen and primecheck sitting in the same directory.
I have a base class ManagerBase that has a generic static function GetManager. There
I have a templated class that is dependent on two template parameters to calculate
Let's say we have a method signature like public static function explodeDn($dn, array &$keys
I have got a C function in a static library, let's call it A,
I have a function that I use to add vectors, like this: public static
I got a Utility module since VB.NET doesn't have static class like C# and
I have a static class that I would like to raise an event as

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.