Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What's the problem with the following code?
C#
class Program
    {
        public delegate string ReturnAStringHandler(string s);

        static void Main(string[] args)
        {
            ReturnAStringHandler sh = ReturnAString;
            ReturnAStringHandler sh2 = () => sh("Hello"); 

            

        }

        public static string ReturnAString(string s)
        {
            return s;
        }
    }


The problem is at this line:
C#
ReturnAStringHandler sh2 = () => sh("Hello");


The compiler says: "Delegate Program.ReturnAStringHandler does not take 0 arguments"



EDIT: Oooooh. It's the signature... Sorry for the stupid question........

What I have tried:

I've tried to understand what is exactly happening.
Posted
Updated 23-Mar-18 20:45pm
v4
Comments
RickZeeland 24-Mar-18 4:44am    
Not a stupid question at all, I still find lambdas hard to work with, but that's probably because I'm an old fart who can't get used to "new style" programming :)

1 solution

Quote:
EDIT: Oooooh. It's the signature... Sorry for the stupid question........

It is indeed!

For the benefit of others with a similar problem, the sh2 declaration requires a parameter:
C#
ReturnAStringHandler sh = ReturnAString;
ReturnAStringHandler sh2 = (x) => sh(x);

Console.WriteLine(sh2("Hello"));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900