loads of useful information, examples and tutorials pertaining to web development utilizing asp.net, c#, vb, css, xhtml, javascript, sql, xml, ajax and everything else...

 



Advertise Here
C-Sharpener.com - Programming is Easy!  Learn Asp.Net & C# in just days, Guaranteed!

Intro to Lambda Operators

by naspinski 3/20/2008 11:06:00 AM

There has been a lot of buzz about lambda operators, so I decided to check them out; turns out they are pretty useful

Not necessarily epic in thier abilities, but lambdas can be a nice way to clean up code and make your programming better in general (if you use them right).  For a quick show of what they can do, compare this function

public int f1(int x, int y, int z)
{
  return (x + y) * z;
}
to this:
Func f2<int, int, int, int> = (x, y, z) => (x + y) * z;
They do basically the same thing, but the second one is much cleaner. It might seem a bit hard to read, but it isn't if you break it down. Think of it like this:
Function with 4 integers (3 in, 1 out) named f2 equals what you get with the inputs x, y and z which go to (x + y) * z
The key here is the 'go to' meaning those variables go into the following function, which in turn, returns to f2... if that makes sense. This is not just a new way to do functions, you can also use lambdas inline which is where they really shine. There is an awesome post here: http://blogs.msdn.com/.../lambda-lambda-lambda.aspx that shows how to use this in an inline generic list sort which chops 5 lines of code down to just 1... that ay not seem like a lot, but it can relly add up.

This is a very basic intro to Lambda Operators and they can do much more, this is just to get your gears turning.
More info: http://msdn2.microsoft.com/.../bb397687.aspx

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

asp.net | c#

Related posts


Comments are closed