delegate signature

T

Tony Johansson

Hi!

At the bottom of this document is some text from the MSDN documentation.
As I have tested and read about delegate the method that is going to be
added to the delegate must be identical as the delegate.
So why does the MSDN documentation write "When you instantiate a delegate,
you can associate its instance with any method with a compatible signature."
I made a test defining a delegate like this delegete void
myDelegateTest(object o);
and tried to add a method with this signature public void Test(int i){}
but that didn't work becuase of the signature

A delegate is a type that defines a method signature. When you instantiate a
delegate, you can associate its instance with any method with a compatible
signature.

//Tony
 
M

mick

Tony Johansson said:
Hi!

At the bottom of this document is some text from the MSDN documentation.
As I have tested and read about delegate the method that is going to be
added to the delegate must be identical as the delegate.
So why does the MSDN documentation write "When you instantiate a delegate,
you can associate its instance with any method with a compatible
signature."
I made a test defining a delegate like this delegete void
myDelegateTest(object o);
and tried to add a method with this signature public void Test(int i){}
but that didn't work becuase of the signature

A delegate is a type that defines a method signature. When you instantiate
a delegate, you can associate its instance with any method with a
compatible signature.

That is not a compatible sig. You couldnt pass an object to a method that
requires
an int even without using a delegate.

mick
 
T

Tony Johansson

mick said:
That is not a compatible sig. You couldnt pass an object to a method that
requires
an int even without using a delegate.

mick

No!
You are thinking wrong it means passing an int to a method expecting an
object would be compatible

The delegate definition was the following
delegete void myDelegateTest(object o);
This means that the return type is void and that the delegete take an object
as the parameter.
So if I tried to add a method with this definition to the delegate
public void Test(int i){} it gives the following error
Error 1 No overload for 'Foo' matches delegate 'Program.MyDelegate'
F:\C#\TonySlask\TonySlask\Program.cs 207 32 TonySlask

So I can't understand why the book says "A delegate is a type that defines a
method signature. When you instantiate a
delegate, you can associate its instance with any method with a compatible
signature."

I have just did what the text says passing a method with a compatible
signature but the compiler is unhappy!

Assume that you have a method definition like this
public void MyMethod(object o){}
then I could call this method in this way
int i = 3;
MyMethod(i);

So what the docs says is incorrect it seems to me that the method must match
the delegete exactly.

//Tony
 
M

mick

Tony Johansson said:
No!
You are thinking wrong it means passing an int to a method expecting an
object would be compatible

Nope! Your method "Test" is - public void Test(int i){}. It is expecting an
int and *not*
an object. Reread what you wrote.
The delegate definition was the following
delegete void myDelegateTest(object o);
This means that the return type is void and that the delegete take an
object as the parameter.
So if I tried to add a method with this definition to the delegate
public void Test(int i){} it gives the following error
Error 1 No overload for 'Foo' matches delegate 'Program.MyDelegate'
F:\C#\TonySlask\TonySlask\Program.cs 207 32 TonySlask

Because there is no method of that name that takes an object (only one that
takes
an int).
So I can't understand why the book says "A delegate is a type that defines
a method signature. When you instantiate a
delegate, you can associate its instance with any method with a compatible
signature."

Yes, but passing an object to a Method expecting an int *isnt* compatible.
I have just did what the text says passing a method with a compatible
signature but the compiler is unhappy!

Assume that you have a method definition like this
public void MyMethod(object o){}
then I could call this method in this way
int i = 3;
MyMethod(i);

Here your method takes an object and you are passing in an int which is the
opposite
of what you were doing above.

mick
 
T

Tony Johansson

mick said:
Nope! Your method "Test" is - public void Test(int i){}. It is expecting
an int and *not*
an object. Reread what you wrote.


Because there is no method of that name that takes an object (only one
that takes
an int).


Yes, but passing an object to a Method expecting an int *isnt* compatible.


Here your method takes an object and you are passing in an int which is
the opposite
of what you were doing above.

mick

What I want to says is what you have specified as the parameter in the
delegate must also be exactly the same for the method that you add to the
delegete.If you for example specify that the delegate take an int then the
method must also take an int.
So no matter what type the delegate use as the parameter the added method to
the delegate MUST also have exctly this parameter type.

So this trash with comaptible format that MSDN is writing is wrong

//Tony
 
W

Willem van Rumpt

Tony said:
What I want to says is what you have specified as the parameter in the
delegate must also be exactly the same for the method that you add to the
delegete.If you for example specify that the delegate take an int then the
method must also take an int.
So no matter what type the delegate use as the parameter the added method to
the delegate MUST also have exctly this parameter type.

So this trash with comaptible format that MSDN is writing is wrong

No, it's correct, although it should add (if it doesn't already), that
the compatibility for parameters only goes for reference types, not
value types (I think this was introduced in C# 2.0).

Your situation

public delegate void DoSomething(object o);

is not compatible with

public void Test(int i){}

and neither with (if that makes things more clear)

public void Test(string s){}

You just cannot put any old object in to something that expects a
string. Mick already explained this to you.

What *does* work is

public delegate void DoSomething(string s);
public void Test(object o){}
 
F

Family Tree Mike

What I want to says is what you have specified as the parameter in the
delegate must also be exactly the same for the method that you add to the
delegete.If you for example specify that the delegate take an int then the
method must also take an int.
So no matter what type the delegate use as the parameter the added method to
the delegate MUST also have exctly this parameter type.

So this trash with comaptible format that MSDN is writing is wrong

//Tony

The following example that works, possibly illustrates what MSDN is saying:

public class Test
{
delegate void del(System.IO.Stream param);

public static void Example(object i)
{
Console.WriteLine(i.ToString());
}

public static void Main()
{
del mytest = new del(Example);
FileStream fs = new FileStream(@"c:\test\SomeFile",
FileMode.Create);
mytest(fs);

Console.WriteLine("Done...");
Console.ReadKey();
}
}

The method in question takes a base class, but the delegate forces the
use through the delegate to take a more specific type of object. I
believe the example you gave worked in reverse with a less specific type
in the delegate declaration.

I also think that declaring a delegate with type int, but calling an
object method fails in my test. I leave it to the smarter guys and gals
on this board to explain that, but suspect it has to do with them being
value types.
 
A

Arne Vajhøj

The delegate definition was the following
delegete void myDelegateTest(object o);
This means that the return type is void and that the delegete take an object
as the parameter.
So if I tried to add a method with this definition to the delegate
public void Test(int i){} it gives the following error
Error 1 No overload for 'Foo' matches delegate 'Program.MyDelegate'
F:\C#\TonySlask\TonySlask\Program.cs 207 32 TonySlask

Ofcourse.

You are adding a method that takes an int as argument to a list
of methods that will be called with any objects (String, XmlDocument,
SqlConnection etc.).
So I can't understand why the book says "A delegate is a type that defines a
method signature. When you instantiate a
delegate, you can associate its instance with any method with a compatible
signature."

I have just did what the text says passing a method with a compatible
signature but the compiler is unhappy!

Assume that you have a method definition like this
public void MyMethod(object o){}
then I could call this method in this way
int i = 3;
MyMethod(i);

So what the docs says is incorrect it seems to me that the method must match
the delegete exactly.

It does not.

The "slack" is just in the opposite direction that what you expect!

Example:

using System;
using System.Xml;

namespace E
{
public class Program
{
public delegate void Foo(object o);
public delegate void Bar(String o);
public static void Main(string[] args)
{
/*
Foo foo = new Foo(FooEx);
foo(new XmlDocument()); // if the previous were allowed
then what would happen here ??
*/
Bar bar = new Bar(BarEx);
bar("ABC"); // this works
Console.ReadKey();
}
public static void FooEx(String o)
{
Console.WriteLine(o.Substring(0, 2));
}
public static void BarEx(Object o)
{
Console.WriteLine(o.ToString());
}
}
}

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top