8

The code examples are not meant to work. They are meant to illustrate ways that I have tried to do what I think may be possible.

// Error: The name First does not exist...
var Contact = new
{
    First = "",
    Last = "",            
    FullName = First + " " + Last
};

// Error: Cannot assign lambda expression to anonymous type property
var Contact = new
{
    First = "",
    Last = "",
    FullName = () => { }
};
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467

2 Answers2

12

Visual Studio 2013:

Anonymous types contain one or more public read-only properties. No other kinds of class members, such as methods or events, are valid. The expression that is used to initialize a property cannot be null, an anonymous function, or a pointer type.

Actually yes you can: MSDN Documentation:

        var fName = "First Name";
        var lName = "Last Name";

        var t = new
        {
            FirstName = "First Name",
            LastName = "Last Name",
            FullName = new Func<string>(() => { return fName + lName; })

        };

Depending on how badly you want TypeSafety, you could do something like this also:

dynamic v = new ExpandoObject();

v.FirstName = "FName";
v.LastName = "LName";
v.FullName = new Func<string>(() => { return v.FirstName + " " + v.LastName; });

Although honestly, I would probably just create a nested type in the method's parent class with those properties and method. Either would work, and the internal class wouldn't messy up the rest of the project if it's only used in there.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
  • 1
    "Anonymous types contain one or more public read-only **properties**." --- did they actually mean [properties](http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx) or something else (presumably fields)? – zerkms Jul 16 '14 at 01:21
  • @zerkms I tried to add a property with get and set. Visual Studio rejected it. – Shaun Luttin Jul 16 '14 at 01:23
  • @Shaun Luttin: I know, it was more like a rhetorical question :-) – zerkms Jul 16 '14 at 01:23
  • 1
    @zerkms No worries. I have a spoonful of Asperger's - everything is literal. – Shaun Luttin Jul 16 '14 at 01:26
  • You don't need to use ExpandoObject - just use a closure: https://gist.github.com/InPermutation/259743e093b75fabe71a – Jacob Krall Jul 16 '14 at 01:34
  • It's odd to me that we can do this easily in JavaScript. I love both languages without understand their differences. – Shaun Luttin Jul 16 '14 at 01:36
  • 1
    What you have to remember is that JavaScript and C# are two totally different languages. They look the same, but are completely different. C# is based on C and static types, and JavaScript is based on LISP. All JavaScript objects are really just HashTables and doesn't enforce much during compile time. – kemiller2002 Jul 16 '14 at 01:39
2

No, you can't.

The reason is that anonymous types are intended to be transient so that you can deal with the results of projections in a concise manner, without having to create an explicit type to hold the results of transforming and projecting a set of data.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120