?
Solved

C#: what is the operator => doing?

Posted on 2010-08-14
4
Medium Priority
?
360 Views
Last Modified: 2013-12-17
Hi Experts:

I saw a section of C# code:
***************************************************************************************  
          System.Threading.ThreadPool.QueueUserWorkItem((state) =>
            {
                System.Threading.Thread.Sleep(3 * 1000);
                Dispatcher.BeginInvoke(() => BusyNote.IsBusy = false); }
            );
****************************************************************************************

Can anybody tell me what is the operator "=>" doing? and what is this section of code doing?
I know it is related to threading , but I don't understand the syntax involving "=>"

This section of code is from a SilverLight 4 application.


Thank you.
0
Comment
Question by:huangs3
4 Comments
 
LVL 30

Accepted Solution

by:
Reza Rad earned 1600 total points
ID: 33439384
=> is LAMBDA operator,
there are lots of useful things you can do with Lambda operator, take a look at these links:
http://msdn.microsoft.com/en-us/library/bb397687.aspx
http://www.switchonthecode.com/tutorials/csharp-tutorial-the-lambda-operator

0
 
LVL 2

Assisted Solution

by:Algol36
Algol36 earned 200 total points
ID: 33439583
Over and above this code use anonimous method equivalent to the following:
...
Dispatcher.BeginInvoke(MyMethod}
...
void MyMethod()
{
  BusyNote.IsBusy = false;
}
0
 
LVL 1

Assisted Solution

by:funazonki
funazonki earned 200 total points
ID: 33440377
It might help to look at a simpler example - the lambda operator allows you to define anonymous functions that can be passed to other functions as parameters. The case you use above is a parameterless function. you can also define functions that take parameters. For example the following code calls the Count function that takes a boolean function as a parameter and returns the number of elements in the list for which the boolean is true.

                List<string> data = new List<string> { "one", "two", "three" };
                Console.WriteLine(data.Count(t=>t.Contains("o")));

The Console.WriteLine will write "2" to the console.
0
 

Author Closing Comment

by:huangs3
ID: 33442343
reza_rad has the earliest and the most important answer while Algol36 and funazonik provided the helpful examples.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question