473,791 Members | 3,179 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)(i nt); // ok
typedef void (*MyDelegate)(i nt 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 1478
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****@microso ft.com.no.spam> wrote in message
news:uT******** ******@TK2MSFTN GP10.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)(i nt); // ok
typedef void (*MyDelegate)(i nt 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****@microso ft.com.no.spam> wrote in message
news:Oz******** ******@TK2MSFTN GP11.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****@microso ft.com.no.spam> wrote in message
news:uT******** ******@TK2MSFTN GP10.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)(i nt); // ok
typedef void (*MyDelegate)(i nt 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)(i nt); // ok
typedef void (*MyDelegate)(i nt 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(ob ject, EventArgs);

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

public delegate void EventHandler(ob ject 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****@microso ft.com.no.spam> wrote in message
news:uT******** ******@TK2MSFTN GP10.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)(i nt); // ok
typedef void (*MyDelegate)(i nt 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 "__unnamed0 00", "__unnamed0 01" 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
3245
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* Sender, System::String* FileName, Handle &File, bool Overwrite, bool IsJournalFile, Error &Result);
0
982
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 based project to call library method asynchronously. Delegate based project returns the control to the client immediately and so the client can perorm other tasks. Every thing works fine but soon as i close the client application, the back ground...
3
1587
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 see no parameters, or a list of parameters that are event dependent. Below is my .NET code to create a class which raises some events. From C++ (using MFC 6.0) I create an instance of the object and IConnectionPointContainer which seems to work.
4
2022
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 of the functions it refers to. So I've been playing around and came up with he following code. I know it doesn't do much, but I just wanted to fit together delegates and parameter passing: //////////////// Code start
4
3393
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 'Delegate as a valid Type declaration for the Value of a Generic Dictionary. I am curious as to why I can compile and use this syntax : it seems to me to violate the requirement that the Value of a Generic Dictionary be a Type Name.
28
4339
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
9994
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
1790
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 MyLovelyDelegate(int parm1, string parm2); 1. Is it true that, in the output assembly, a new class will be created that inherits from System.MulticastDelegate - and the name of that class is
11
8337
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 device_t { const device_backend_t *backend; ... } device_t; typedef struct device_backend_t {
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10147
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9987
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9023
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6770
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.