Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5. // M wants to get an instance via Func
  6. static void M(Func<string> factory)
  7. {
  8. Console.WriteLine(factory());
  9. }
  10.  
  11. static void Main()
  12. {
  13. // Caller wants to pass just a single instance to M
  14. string s = Console.ReadLine();
  15.  
  16. // By using a lambda () => s
  17. // this cause a heap allocation for an anonymous class
  18. M(() => s);
  19.  
  20. // On the other hand, by using curried delegate
  21. // no anonymous class
  22. M(s.Identity);
  23. }
  24. }
  25.  
  26. static class TrickyExtension
  27. {
  28. // Returns the parameter as-is
  29. public static T Identity<T>(this T x) => x;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement