ITQuants blog

C# lambda operator and other new functionalities on .NET 3.5

Feb 22

Written by:
2/22/2012 2:33 PM  RssIcon

1 - The lambda operator permits the definition of anonymous functions, needed for instance when you want to define quickly a callback. It does not introduce a new functionality, since it already existed in .Net 2.0 using the delegate keyword. It is just only a new syntax.

For instance, compiling with VS2005, some code which looked like that:

workItemsGroup.QueueWorkItem(delegate { act(); Interlocked.CompareExchange(ref choiceIndex._index, value, -1); anActionCompleted.Set(); });

Could look like that using the lambda operator:

workItemsGroup.QueueWorkItem(state => (  act(); Interlocked.CompareExchange(ref choiceIndex._index, value, -1); anActionCompleted.Set(); ));

Even if some developers think that it is a great advance, I'm not really sure. Moreover, looking at the code and the use of anonymous functions, it replicates often the same code, using the banished copy-paste design pattern....

2- In other new functionalities is the declaration of properties using the getter and setter, without declaring the member variable. This new syntax is more interesting and helps the programmer to develop faster the code.

Sample (and that's all):

string Name { get; set; }



3- Overloading the intialisation of members in the constructor without declaring new constructors: don't need anymore to add new variables as parameters in the constructor if we need to change the initialisation on some instances. We just have to declare the initialisation when declaring the instance:

MyClass _class = new MyClass() { m_myVariable = "New string; };

Search blog