Created
July 16, 2014 01:33
-
-
Save InPermutation/259743e093b75fabe71a to your computer and use it in GitHub Desktop.
Stupid C# anonymous objects tricks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
(From http://stackoverflow.com/questions/24770648/can-we-add-a-method-to-an-anonymous-type-that-refers-to-the-anonymous-types-mem)