4

When we create a delegate in C#, to point a function with a definite signature (parameter set), it asks us to specify the identifier also for each type.

public delegate void myDelegate(int x, int y);  

if I try to write this prototype declaration as:

public delegate void myDelegate(int, int)

it shows a compile time error saying identifier expected.

But according to me, when we are just specifying the prototype for the method, why compiler needs an identifier to distinguish between two methods with different signature:

public delegate void firstDelegate(int);

and

public delegate void secondDelegate(int, int);

are the sufficient and clear declaration to distinguish between them. I think so

I think you people got me??

7
  • 1
    Don't "define a delegate". Use System.Action<int,int>. Don't try to reinvent the wheel. Commented Apr 6, 2014 at 20:39
  • I really hate to be the guy who words stuff like this, but "because that's how the language is" If I had to guess, I'd say that it's so that intellisense could give helpful data and variable names as you pass them into the delegate Commented Apr 6, 2014 at 20:41
  • @HighCore You would hope everyone's using the latest .NET version, but may still be worth answering for older revisions that don't have System.Action<...>. Commented Apr 6, 2014 at 20:41
  • @jonathanlonowski then upgrade the .Net version. this is not java. Commented Apr 6, 2014 at 20:42
  • 1
    I personally consider e.g. a delegate NetEventHandler(Socket sock, NetEvent event) much more readable, than System.Action<Socket,NetEvent> Commented Apr 6, 2014 at 20:46

1 Answer 1

8

It can make a difference at the point of invocation. For example:

using System;

class Test
{
    delegate void Foo(int x, int y);

    static void Main()
    {
        Foo foo = (x, y) => Console.WriteLine("x={0}, y={1}", x, y);
        foo(x: 5, y: 10);
        foo(y: 10, x: 5);
    }
}

The output is x=5, y=10 for both lines, because the arguments use names rather than positions. Even though C# only gained named arguments in C# 4, VB.NET has had them for much longer.

Now of course it didn't have to be like that. It could have been designed so that delegates didn't have named parameters in the first place - but when everything else you invoke has named parameters, why would you want to make delegates different?

Would you propose the same for interface methods and abstract methods, by the way? Again, there's no direct use of the parameters in the declarations, as they're just signatures.

Note that parameter names help in terms of readability and allow the parameters to be documented more easily too.

Sign up to request clarification or add additional context in comments.

4 Comments

+1... Note that sample is not historically consistent :).. Question was essentially why .Net 1.0 (2002+) required names for parameters of delegates and you show how it could be useful when writing Lambda expression (2008+) and use named parameters (2010+)
@AlexeiLevenkov: Don't confuse the framework with the C# language. I know that VB had optional parameters long before C# did - it wouldn't surprise me if it had named arguments too, although I haven't verified that. Even if it didn't, other languages could have done. The fact that the parameters had names means that they could be used, even if not in C#.
Actually your comment could be better answer :) - indeed VB.Net supported named parameters since long time - so names on delegates were needed well before C# is able to use them.
@AlexeiLevenkov: Will add that bit. I hadn't found a reference, so didn't want to claim it. As a point of pedantry, you mean named arguments - all parameters are named... (Microsoft documentation often gets this terminology wrong, unfortunately.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.