Skip to content

Instantly share code, notes, and snippets.

@InPermutation
Created July 16, 2014 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InPermutation/259743e093b75fabe71a to your computer and use it in GitHub Desktop.
Save InPermutation/259743e093b75fabe71a to your computer and use it in GitHub Desktop.
Stupid C# anonymous objects tricks
// First declare the "type" of Contact
var Contact = new
{
First = (string)null,
Last = (string)null,
FullName = (Func<string>)null,
};
// Then, assign a closure over Contact to FullName to implement the "method"
Contact = new
{
First = "Jacob",
Last = "Krall",
FullName = (Func<string>)(() => Contact.First + " " + Contact.Last)
};
Console.WriteLine(Contact.FullName()); // "Jacob Krall"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment