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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:38:48+00:00 2026-05-23T17:38:48+00:00

I have 2 overloaded C# functions like this: private void _Insert(Hashtable hash, string tablename,

  • 0

I have 2 overloaded C# functions like this:

private void _Insert(Hashtable hash, string tablename, Func<string, object[], SqlCommand> command)
private void _Insert(Hashtable hash, string tablename, Func<string, object[], OleCommand> command)

Basically one using OleCommand and the other SqlCommand for the return value of the function.

But the ugly thing about this is that I have to cast the function pointer to the correct type even though i feel the compiler should be able to resolve it without problems:

class RemoteDatabase
{    
      public SqlCommand GetCommand(string query, object[] values);
}

_Insert(enquiry, "Enquiry", (Func<string, object[], SqlCommand>)(_RemoteDatabase.GetCommand));

Is there any way tell the compiler to be smarter so that I don’t have to do the type casting? Or did I do anything wrong?

EDIT:
Added a bounty because I am really interested to learn. Thanks for any advice.

  • 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-23T17:38:49+00:00Added an answer on May 23, 2026 at 5:38 pm

    While not answering your question directly, I did come across the following while writing up a test case, you can get it to compile by wrapping the call in another lambda. Which removes the explicit cast at the cost of another method call (at least I think so, haven’t looked at the IL yet)

    class RemoteDatabase
    {
        public int GetCommand(){return 5;}
    }
    
    class Program
    {
    
        static void Main(string[] args)
        {
            var rd = new RemoteDatabase();
    
            // Overloaded(1, rd.GetCommand); // this is a compile error, ambigous
    
            Overloaded(1, () => rd.GetCommand()); // this compiles and works
    
            Console.ReadLine();
        }
    
        static void Overloaded(int paramOne, Func<int> paramFun)
        {
            Console.WriteLine("First {0} {1}", paramOne, paramFun());
        }
    
        static void Overloaded(int paramOne, Func<string> paramFun)
        {
            Console.WriteLine("Second {0} {1}", paramOne, paramFun());
        }
    }
    

    EDIT-
    I found this post by Eric Lippert that answers this question

    An interesting fact: the conversion rules for lambdas do take into
    account return types. If you say Foo(()=>X()) then we do the right
    thing. The fact that lambdas and method groups have different
    convertibility rules is rather unfortunate.

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

Sidebar

Related Questions

I have two overloaded methods, one with an optional parameter. void foo(string a) {
i have an overloaded operator << trying to make it work like this mystream<<hex<<10;
Problem Approch I have a class like this with a overloaded constructors Code <?php
I have a bunch of overloaded functions that operate on certain data types such
I have a boost variant of looking like this: typedef boost::variant<int, float, double, long,
I have a class which has two overloaded functions. How do I export it
i have a priority queue and i have defined like this: priority_queue<Node*,vector<Node*>,greater<Node*>> myQueue; i
We have some code that looks like this: class Serializer { public: template<class Type>
Currently, I have a static factory method like this: public static Book Create(BookCode code)
I have written a template function like this: template <class T> const T& max1(const

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.