SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top