Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No overload of WriteLine matches delegate with same signature

Tags:

c#

delegates

I was recently writing some code to do WriteLine using a delegate in .NET 3.5. It was all going well using various different Action delegates until I came to the overload of WriteLine which is

public static void WriteLine(string format, object arg0, object arg1, object arg2, object arg3)

As I'm using .NET 3.5 there is no Action<T1, T2, T3, T4, T5> delegate so I quicly wrote my own:

public delegate void Action<T1, T2, T3, T4, T5>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);

I then went to assign WriteLine to this method Action<string, object, object, object, object> action = Console.WriteLine; and I got the error message

No overload for 'WriteLine' matches delegate 'Action<string,object,object,object,object>'

I thought this must be caused as a result of some generics issue so I explicitly declared a new delegate:

public delegate void WriteLineAction(string format, object arg0, object arg1, object arg2, object arg3);

I then tried assigning WriteLine to this new method and again got the error message:

No overload for 'WriteLine' matches delegate 'WriteLineAction'

The even stranger thing is that if I right click on the Console.WriteLine call that's in error and say "Go To Definition" it takes me to the right version of WriteLine in the metadata, so why is it not able to match the method to the delegate. Am I doing something really stupid here or is this a known issue / feature?

like image 597
Robert Davey Avatar asked Dec 28 '25 14:12

Robert Davey


1 Answers

If you look at the docs for this overload it shows:

This API is not CLS-compliant. The CLS-compliant alternative is WriteLine(String, Object[]).

and if you look at the C# tab in the signature, it says:

C# does not support methods that use variable length arguments (varargs). The compiler automatically resolves calls to this method to the same method that uses a parameter array.

The C++ tab shows the signature as:

public:
static void WriteLine(
    String^ format, 
    Object^ arg0, 
    Object^ arg1, 
    Object^ arg2, 
    Object^ arg3, 
    ...
)

I strongly suspect this is the issue. In Reflector it shows up as:

public static void WriteLine(string format, object arg0, object arg1,
    object arg2, object arg3, __arglist)
like image 119
Jon Skeet Avatar answered Dec 31 '25 18:12

Jon Skeet