Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am writing delegates as like this.

C#
delegate void MyMethod(string arg1, string arg2);
MyMethod mm;


I don't know why it needs two lines to declare a single delegate. If my class has 20 delegates, I need to write 40 line of code.
Can anybody tell me a way to write this in one line of code ?
Thanks in Advance.
Posted
Comments
Thomas Daniels 23-Jun-14 2:19am    
Why do you need 20 delegates?
Sergey Alexandrovich Kryukov 23-Jun-14 2:31am    
Reasonable question. I can imagine 20 delegate instances (or, more likely, indefinitely big number of them, created on the fly), but why 20 different delegate types? However, I know at least one case what doing so is very wise: unification of some available unmanaged API for more than one instruction set platform under a single "Any CPU" assembly. Could you beat that?
—SA
Sergey Alexandrovich Kryukov 23-Jun-14 2:33am    
OP answered below...
—SA
Thomas Daniels 23-Jun-14 2:36am    
Ok, thanks for notifying.
Yesudass Moses 23-Jun-14 2:31am    
I have a class that works in a thread, I need to trigger the functions(events) inside the main form if needed. Moreover I dont want to deal with form properties directly from the class, instead calling the function, that updates the form..

The first line is the definition of the delegate (you can compare it to a class, struct or enum definition).
The second line declares a variable which has your delegate as type.
These are not the same things. You cannot mix them in a single statement.
Cheers.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Jun-14 2:24am    
Not quite. Please see my answer.
—SA
Chasing minimal number of lines is just silly. You should try to write the number of lines promoting better readability, never repeating the same code, and so on.

However, there is absolutely no way to write those delegate types like the one you wrote. You can solve such problems using generics. You don't even have to define generic types or methods: several definitions in .NET BCL already cover some 99% of practically important cases including yours. This is what you can do:
C#
System.Action<string, string> mm;

That's not all. You don't even need to write a separate (named) method if you want immediately initialize mm> with some delegate instance. Here is what you can do, for example:
C#
System.Action<string, string> mm = new System.Action<string, string>((s1, s2) => {
    System.Console.WriteLine(@"First argument: ""{0}""; second argument:""{1}""", s1, s2); // or anything else
});

To understand how it all works, read on the following topics: 1) generics, 2) anonymous methods; 3) lambda expressions and methods; 4) as a bonus: closures.

By the way, I would advise to read some language and .NET manual from the beginning to the end before writing anything serious (beyond learning exercises) and trying to improve something. First, you need to know what is already available.

—SA
 
Share this answer
 
Comments
Yesudass Moses 23-Jun-14 2:29am    
Is ther eany perfomance difference between delegates and Actions or Func ?
Sergey Alexandrovich Kryukov 23-Jun-14 11:46am    
Action is just the generic delegate type declaration, so the question about performance is not applicable.
And the question about performance comparison is not quite correct. If you create a delegate instance, add just one handler and invoke delegate instance, it would be a bit slower than the call of a function. But the delegate mechanism does a lot more than a function call. And also, this is the inversion of control (read about it). If you need this functionality, you have to use it.

[EDIT]

Sorry, I did not realize that you did not mention function call. Func is a set of generic delegate type declaration, same as action but with non-void return. All those generics just generate a set of delegate type declarations, so it may not affect performance in principle. This is the same as if you declared those delegate type yourself, generic or not. Type declaration is not a runtime code. More exactly, it takes time during runtime, but only in the sense of the JIT operation.

—SA
Thomas Daniels 23-Jun-14 2:38am    
Good answer, a 5.
Sergey Alexandrovich Kryukov 23-Jun-14 18:24pm    
Thank you very much.
—SA
Yesudass Moses 24-Jun-14 3:32am    
Thanks @Sergey

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900