473,324 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

why the delegate declaration needs named parameters?

Hello,

I wonder why the delegate declaration needs named parameters?

public delegate void MyDelegate( int a, int b ); // ok
public delegate void MyDelegate( int, int ); // compiler error

C allows to define both:

typedef void (*MyDelegate)(int); // ok
typedef void (*MyDelegate)(int a); // ok

in fact, the delegates are only signatures, so the names should be
irrelevant. it could be even more elegant to use nameless signatures,
because now it looks rather confusing:

public delegate void MyDelegate( int a, int b ); // names: a, b
public void a_Method( int x, int y ) {} // names: x, y

.... MyDelegate m = new MyDelegate( a_Method ); // ok, but there's slight
confusion

while it could be:

public delegate void MyDelegate( int, int ); // a signature only
public void a_Method( int x, int y ) {} // names: x, y

.... MyDelegate m = new MyDelegate( a_Method );

thanks in advance for enlightening me,
Wiktor Zychla
Nov 15 '05 #1
9 1447
Hi Wiktor,

I guess the part of the answer is for the methods BeginInvoke and EndInvoke
which are generated by (compiler) for each delegate - for naming parameters.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Wiktor Zychla" <ie****@microsoft.com.no.spam> wrote in message
news:uT**************@TK2MSFTNGP10.phx.gbl...
Hello,

I wonder why the delegate declaration needs named parameters?

public delegate void MyDelegate( int a, int b ); // ok
public delegate void MyDelegate( int, int ); // compiler error

C allows to define both:

typedef void (*MyDelegate)(int); // ok
typedef void (*MyDelegate)(int a); // ok

in fact, the delegates are only signatures, so the names should be
irrelevant. it could be even more elegant to use nameless signatures,
because now it looks rather confusing:

public delegate void MyDelegate( int a, int b ); // names: a, b
public void a_Method( int x, int y ) {} // names: x, y

... MyDelegate m = new MyDelegate( a_Method ); // ok, but there's slight
confusion

while it could be:

public delegate void MyDelegate( int, int ); // a signature only
public void a_Method( int x, int y ) {} // names: x, y

... MyDelegate m = new MyDelegate( a_Method );

thanks in advance for enlightening me,
Wiktor Zychla

Nov 15 '05 #2
My guess:

They probably use the same grammer rule for the bit within
brackets for both delegate declarations and actual
function declarations. Probably done to reduce the size of
the language grammer, although it might be because some
IDEs automatically pick up function signatures, and seeing
something like func(int iIndex, int iOffset) popping up is
more useful than seeing func(int,int). .

C in contrast has a lot of historical quirks. To my
knowledge, this is valid Ansi C (a hangover from 1970's
K&R C):

int double(val) int val; { return val>>2; }
Nov 15 '05 #3
> I guess the part of the answer is for the methods BeginInvoke and
EndInvoke
which are generated by (compiler) for each delegate - for naming

parameters.

could not the compiler produce any irrelevant names itself?
I guess you never need the names in C#.
Nov 15 '05 #4
> They probably use the same grammer rule for the bit within
brackets for both delegate declarations and actual
function declarations. Probably done to reduce the size of
the language grammer, although it might be because some
IDEs automatically pick up function signatures, and seeing
something like func(int iIndex, int iOffset) popping up is
more useful than seeing func(int,int). .
that sounds reasonable but making the language spec to follow the IDE's
requirements is not advisable. what I think is that the C# could accept both
forms:

delegate int MyDelegate( int a, int b );
and
delegate int MyDelegate( int, int );
C in contrast has a lot of historical quirks. To my


that's true. I just wanted to show that the nameless definition is clearer
and less confusing.
Nov 15 '05 #5
Hi Wiktor,
"Wiktor Zychla" <ie****@microsoft.com.no.spam> wrote in message
news:Oz**************@TK2MSFTNGP11.phx.gbl...
I guess the part of the answer is for the methods BeginInvoke and

EndInvoke
which are generated by (compiler) for each delegate - for naming

parameters.

could not the compiler produce any irrelevant names itself?


But then they would be needless. I think it helps to have meaningful names
as parameters
Isn't better to have string firstName, string lastName then string, string
or string x, string y?

--
Miha Markic - DXSquad/RightHand .NET consulting & software development
miha at rthand com

Nov 15 '05 #6
I can't answer why C# requires the names, but I can tell you that if it
didn't, I'd use them anyway.

There's a lot to be said for the kind of documentation that Intellisense
provides, and meaningful parameter names are great reminders of what goes
where.

"Wiktor Zychla" <ie****@microsoft.com.no.spam> wrote in message
news:uT**************@TK2MSFTNGP10.phx.gbl...
Hello,

I wonder why the delegate declaration needs named parameters?

public delegate void MyDelegate( int a, int b ); // ok
public delegate void MyDelegate( int, int ); // compiler error

C allows to define both:

typedef void (*MyDelegate)(int); // ok
typedef void (*MyDelegate)(int a); // ok

in fact, the delegates are only signatures, so the names should be
irrelevant. it could be even more elegant to use nameless signatures,
because now it looks rather confusing:

public delegate void MyDelegate( int a, int b ); // names: a, b
public void a_Method( int x, int y ) {} // names: x, y

... MyDelegate m = new MyDelegate( a_Method ); // ok, but there's slight
confusion

while it could be:

public delegate void MyDelegate( int, int ); // a signature only
public void a_Method( int x, int y ) {} // names: x, y

... MyDelegate m = new MyDelegate( a_Method );

thanks in advance for enlightening me,
Wiktor Zychla

Nov 15 '05 #7
Wiktor,

It must be something specific to the C# compiler given
that in managed C++ you are able to do the following:

__delegate int SomeCalculation(int);

Obviously by the time it has become MSIL it doesn't really
matter if there were names or not. What I haven't tried
that would be interesting is to use the above listed
delegate compiled into a C++ dll from C# and see what
names are chosen (if any) for the parameters.

JM

-----Original Message-----
Hello,

I wonder why the delegate declaration needs named parameters?
public delegate void MyDelegate( int a, int b ); // ok
public delegate void MyDelegate( int, int ); // compiler error
C allows to define both:

typedef void (*MyDelegate)(int); // ok
typedef void (*MyDelegate)(int a); // ok

in fact, the delegates are only signatures, so the names should beirrelevant. it could be even more elegant to use nameless signatures,because now it looks rather confusing:

public delegate void MyDelegate( int a, int b ); // names: a, bpublic void a_Method( int x, int y ) {} // names: x, y
.... MyDelegate m = new MyDelegate( a_Method ); // ok, but there's slightconfusion

while it could be:

public delegate void MyDelegate( int, int ); // a signature onlypublic void a_Method( int x, int y ) {} // names: x, y
.... MyDelegate m = new MyDelegate( a_Method );

thanks in advance for enlightening me,
Wiktor Zychla
.

Nov 15 '05 #8
There is no language requirement that delegate parameters be named - we
could easily have spec'd C# so that they weren't required.

They are required because they improve readability considerably. When you go
to use a delegate, it's much easier to tell what's going on. Consider the
standard windows forms event handler:

public delegate void EventHandler(object, EventArgs);

that doesn't give you much information about the first parameter, while

public delegate void EventHandler(object sender, EventArgs e);

This is generally more of an issue when the type is one that could be used
in a lot of ways - such as object, int, etc.

Hope that helps.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Wiktor Zychla" <ie****@microsoft.com.no.spam> wrote in message
news:uT**************@TK2MSFTNGP10.phx.gbl...
Hello,

I wonder why the delegate declaration needs named parameters?

public delegate void MyDelegate( int a, int b ); // ok
public delegate void MyDelegate( int, int ); // compiler error

C allows to define both:

typedef void (*MyDelegate)(int); // ok
typedef void (*MyDelegate)(int a); // ok

in fact, the delegates are only signatures, so the names should be
irrelevant. it could be even more elegant to use nameless signatures,
because now it looks rather confusing:

public delegate void MyDelegate( int a, int b ); // names: a, b
public void a_Method( int x, int y ) {} // names: x, y

... MyDelegate m = new MyDelegate( a_Method ); // ok, but there's slight
confusion

while it could be:

public delegate void MyDelegate( int, int ); // a signature only
public void a_Method( int x, int y ) {} // names: x, y

... MyDelegate m = new MyDelegate( a_Method );

thanks in advance for enlightening me,
Wiktor Zychla

Nov 15 '05 #9
> Obviously by the time it has become MSIL it doesn't really
matter if there were names or not. What I haven't tried
that would be interesting is to use the above listed
delegate compiled into a C++ dll from C# and see what


I've tested that.
the parameters are named "__unnamed000", "__unnamed001" and so on.
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eugene Mayevski | last post by:
Hello, I am trying to declare in C++ and use in C# a delegate which contains "ref" and "out" parameters. I declare a delegate as: public __delegate void SolFSCreateFileEvent(SolFSStorage*...
0
by: Irfan | last post by:
I have a Delegate based Project that contain classes whose purpose is to instantiate objects of another Project (or Library) using Delegates. I have a client application that uses this Delegate...
3
by: Michael Tissington | last post by:
I'm confused by documentation and examples on using Delegate to create Events for use with COM In some situation I see a parameter list of (sender as Object, e as EventArgs) and other times I...
4
by: ^MisterJingo^ | last post by:
Hi all, I've been trying to get my head around delegates. The book i'm using had a single example, not much explaination, and didn't show how to set up a delegate and pass variables in and out...
4
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
10
by: Nathan Laff | last post by:
I have a custom attribute which i use for fields in an enum. I want to pass around a delegate in these things. so i want to do something like this Is this possible? i'm having no luck.
4
by: Bob Cramer | last post by:
I don't have a copy of Reflector handy :-( so I'd appreciate if someone could confirm (or clarify) the following. In consideration of the following delegate declaration... delegate string...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.