Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# function pointer in overloaded function

Tags:

c#

overloading

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.

like image 469
Jake Avatar asked May 03 '11 07:05

Jake


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

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.

like image 199
BrandonAGr Avatar answered Oct 13 '22 00:10

BrandonAGr


EDIT: This is caused by process defined in section Overload resolution of C# specification. Once it gets the set of applicable candidate function members it cannot choose 'best function' because it isnt taking return type in the account during overload resolution. Since it cannot choose best function ambigous method call error occures according to spec.

However if your goal is to simplify method calls and avoid long casting to func you can use generics for and complicate _Insert method by a little bit like this for example:

public  void Main()
    {
        _Insert(new Hashtable(), "SqlTable", F1);
        _Insert(new Hashtable(), "OleTable", F2);
    }

    private static SqlCommand F1(string name, object[] array)
    {
        return new SqlCommand();
    }

    private static OleDbCommand F2(string name, object[] array)
    {
        return new OleDbCommand();
    }

    private void _Insert<T>(Hashtable hash, string tablename, Func<string, object[], T> command) 
    {
        if (typeof(T) == typeof(SqlCommand)) {
            SqlCommand result = command(null, null) as SqlCommand;
        }
        else if (typeof(T) == typeof(OleDbCommand)) {
            OleDbCommand result = command(null, null) as OleDbCommand;
        }
        else throw new ArgumentOutOfRangeException("command");
    }

Notice simplified method calls

_Insert(new Hashtable(), "OleTable", F1);
_Insert(new Hashtable(), "OleTable", F2);

that compile just fine

like image 40
Valentin Kuzub Avatar answered Oct 13 '22 00:10

Valentin Kuzub