Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

casting delegate

Tags:

I'm pretty new to c#, so my question might be simple, but here goes.

I've have been trying to work with delegates, and is kinda stuck with this problem.

.....     public delegate double delegateA();     public delegate double delegateB();      public static double myFunc()     {         return 0;     }     public static delegateA myTest()     {         return myFunc;     }      static void Main(string[] args)     {         delegateB myFuncDelegate;          myFuncDelegate = myTest();  // <-- Error: Cannot implicitly convert type....     } ..... 

I don't know how to make this conversion work, unless using the same delegate as type. But in my project, it would be more pretty for the delegate's to have different names (as they exist in different classes.

Hope you can help me.

like image 774
JakobJ Avatar asked Aug 12 '10 06:08

JakobJ


People also ask

What is single cast delegate?

SingleCast Delegates refer to a single method with matching signature. SingleCast Delegates derive from the System.Delegate class. Multi Cast Delegate: This is a kind of delegates that can refer to multiple methods that have the same signature at one time.

What is a delegate method?

A delegate method is a method that the delegate object is expected to implement. Some delegate methods are required, while some are not. In IOS, most delegates are expected to conform to an Objective-C protocol; the protocol declaration will tell you which methods are optional and which are required.

What is meant by multicast delegate?

The multicast delegate contains a list of the assigned delegates. When the multicast delegate is called, it invokes the delegates in the list, in order. Only delegates of the same type can be combined. The - operator can be used to remove a component delegate from a multicast delegate.

Can delegates be private?

Just like classes and interfaces, we can declare delegates outside of classes or nested within classes. We can mark them private , public , or internal .


2 Answers

You can't convert between delegates like that directly. What you can do is make a new delegate from an existing, compatible one. So if you change your code to:

 delegateB myFuncDelegate = new delegateB(myTest()); 

that will work. (Note that "compatibility" doesn't necessarily meant that the signatures are identical - see the language specification for details.)

Just to make this slightly clearer to other readers, here's a simpler complete example, which doesn't need any method calls.

// Two delegate types with the same signature delegate void Foo(); delegate void Bar();  class Test {     static void Main()     {         // Actual value is irrelevant here         Foo foo = null;          // Error: can't convert like this         // Bar bar = foo;          // This works fine         Bar bar = new Bar(foo);     } } 

Note that there's one exception to this "no conversions" rule - generic variance in C# 4. For example, in C# 4 you can write:

 Action<object> foo = x => Console.WriteLine(x.GetHashCode());  Action<string> bar = foo; 

... because Action<T> is contravariant in T (so it's actually declared as Action<in T>). This is a reference conversion - it doesn't create a new delegate like the first sample does. However, this isn't available for merely "compatible" delegates - only generic ones.

like image 148
Jon Skeet Avatar answered Oct 11 '22 15:10

Jon Skeet


Also, not exactly what you asked about, but interestingly -- this doesn't work:

Func<double> func_double = () => 1; Func<object> func_object;  func_object = func_double; 

But this does:

Func<string> func_string = () => "hello"; Func<object> func_object;  func_object = func_string; 

Difference being the string example is using reference types which can cast to an object, while a double must be boxed thereby blocking a direct cast

like image 39
Malachi Avatar answered Oct 11 '22 13:10

Malachi