|
-
March 22nd, 2012, 04:30 PM
#1
[Please Teach Me] C# Delegates and Lambda Expressions
I have some idea about C#'s Delegates and Lambda Expressions. Please give me a detailed but self explanatory contribution to this.
-
March 22nd, 2012, 06:05 PM
#2
Re: [Please Teach Me] C# Delegates and Lambda Expressions
This sort of question is too broad to be answered. Do you have a specific question about delegates and lambda expressions?
Otherwise, you should read the top hits on Google for those topics:
https://encrypted.google.com/search?q=C%23+delegates
https://encrypted.google.com/search?...da+expressions
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
March 27th, 2012, 02:11 PM
#3
Re: [Please Teach Me] C# Delegates and Lambda Expressions
I talk a bit about delegates (and events) here, to help another user: take a look. (After the first paragraph, where I talk about value types and reference types.)
(See also: official MSDN docs here, and a a tutorial here.)
The key point to remember about delegates is that they can represent any method with a specific signature (return type/parameter list), that they can store references to one or more such methods, and that they can be used to call them whenever it seems appropriate. (Take events: their underlying delegate keeps track of all assigned handler methods (and there could be more than one), so that, when the event happens, they can all be notified (called).)
Now, that was about delegate types.
There are also anonymous methods, which are created using the delegate keyword (maybe a bit inappropriately chosen, but, what can one do...)
This is a language mechanism which enables you to basically declare a method almost anywhere in code, without specifying it's name, and assign it to a delegate so that it can be used somewhere.
One reason is convenience - for example, if you need to assign some simple behavior as an event response, then you don't have to go through the whole process of creating a method, finding the appropriate class for it, and assigning it to an event, as an event handler (more on that in post I linked to). Instead, with anonymous methods, you can just do it on the go - inline if you will.
For example:greetButton.Click += delegate { MessageBox.Show("Hi!"); }; // ignores any arguments passed to it
Anonymous methods can accept parameters as well. See the MSDN docs on them form more detail.
Then there's this thread where I got a bit chatty about lambdas and LINQ.
Lambda expressions a lot like anonymous methods - they only use a different syntax, and offer more flexibility. But in essence, a lambda is just a nameless function.
Basically, this is the anatomy of your standard lambda expression:(parameter_list) => lambda_body // read "=>" as "goes to"
While with methods you have:
Code:
return_type Method_Name([I]parameter_list[/I])
{
method_body // <==> lambda_body of lambdas
}
Lambda's return type is inferred automatically from the type of the object you return from it. Also, you usually only type in the names of the parameters (the names you're going to use in lambda body), since the type is inferred from the delegate it gets assigned to. The lambda_body can be a single statement, or a regular code_block, as is the case with methods.
Read more at MSDN.
Also, see this, and this.
As a final piece of information, lambda's are often used in conjunction with LINQ.
After reading all that, if you have some specific questions, ask here.
-
March 30th, 2012, 08:25 PM
#4
Re: [Please Teach Me] C# Delegates and Lambda Expressions
Nice post. Thanks for the tutorial. God bless you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|