Solved

C#: what is the operator => doing?

Posted on 2010-08-14
4
349 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
Comment Utility
=> 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
Comment Utility
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
Comment Utility
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
Comment Utility
reza_rad has the earliest and the most important answer while Algol36 and funazonik provided the helpful examples.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
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 …
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now