Solved

C#: what is the operator => doing?

Posted on 2010-08-14
4
353 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 400 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 50 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 50 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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