Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- // M wants to get an instance via Func
- static void M(Func<string> factory)
- {
- Console.WriteLine(factory());
- }
- static void Main()
- {
- // Caller wants to pass just a single instance to M
- string s = Console.ReadLine();
- // By using a lambda () => s
- // this cause a heap allocation for an anonymous class
- M(() => s);
- // On the other hand, by using curried delegate
- // no anonymous class
- M(s.Identity);
- }
- }
- static class TrickyExtension
- {
- // Returns the parameter as-is
- public static T Identity<T>(this T x) => x;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement