?
Solved

C#: what is the operator => doing?

Posted on 2010-08-14
4
Medium Priority
?
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

743 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