PageRenderTime 23ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/DelegatesEventsAndLambdas/4. Using Lambdas.cs

http://github.com/DrivenSoftware/Driven.SimpleSamples.Dotnet
C# | 85 lines | 35 code | 17 blank | 33 comment | 0 complexity | 070201384b5ce8088be3ce9eb505064d MD5 | raw file
  1. // ReSharper disable RedundantLambdaParameterType
  2. /*
  3. * This example demonstrates the use of a lambdas. Step through the test methods and see how they work.
  4. *
  5. * Terminology:
  6. *
  7. * 1. Lambda - A lambda expression is an anonymous function that can contain expressions and statements,
  8. * and can be used to create delegates or expression tree types. All lambda expressions use
  9. * the lambda operator =>, which is read as "goes to". The left side of the lambda operator
  10. * specifies the input parameters (if any) and the right side holds the expression or statement
  11. * block. The lambda expression [[ x => x * x ]] is read "x goes to x times x."
  12. *
  13. * Observe:
  14. *
  15. * 1. The most long-winded form of a lambda expression goes like this:
  16. *
  17. * (explicitly-typed-parameter-list) => { statements }
  18. *
  19. * 2. The compiler is smart enough to reduce this down to the bare necessities:
  20. *
  21. * parameter-name => expression
  22. */
  23. using System;
  24. using NUnit.Framework;
  25. using System.Linq;
  26. namespace DelegatesAndLambdas
  27. {
  28. [TestFixture]
  29. public class Lambdas
  30. {
  31. // Declaration
  32. public delegate void SimpleDelegate(double parm1);
  33. [Test]
  34. public void Use_a_simple_lambda_expression()
  35. {
  36. // Instantiation - anonymous method
  37. SimpleDelegate anon = delegate(double number) { Console.WriteLine(Math.Sqrt(number)); };
  38. // Instantiation - lambda, full syntax
  39. SimpleDelegate lambda1 = (double number) => Console.WriteLine(Math.Sqrt(number));
  40. // Instantiation - lambda, shorthand
  41. SimpleDelegate lambda2 = (number) => Console.WriteLine(Math.Sqrt(number));
  42. // Instantiation - lambda, concise
  43. SimpleDelegate lambda3 = number => Console.WriteLine(Math.Sqrt(number));
  44. // Invocation
  45. anon(49);
  46. lambda1(49);
  47. lambda2(49);
  48. lambda3(49);
  49. }
  50. [Test]
  51. public void Use_a_lamda_expression_that_returns_a_result()
  52. {
  53. // Instantiation - anonymous method
  54. Func<string, int> returnLengthAnon = delegate(string text) { return text.Length; };
  55. // Instantiation - lambda, full syntax
  56. Func<string, int> returnLengthLambda1 = (string text) => { return text.Length; };
  57. // Instantiation - lambda, shorthand
  58. Func<string, int> returnLengthLambda2 = (string text) => text.Length;
  59. // Instantiation - lambda, concise
  60. Func<string, int> returnLengthLambda3 = text => text.Length;
  61. // Invocation
  62. Console.WriteLine(returnLengthAnon("Hello Anonymous World"));
  63. Console.WriteLine(returnLengthLambda1("Hello Lambda World"));
  64. Console.WriteLine(returnLengthLambda2("Hello Lambda World"));
  65. Console.WriteLine(returnLengthLambda3("Hello Lambda World"));
  66. }
  67. }
  68. }