Search code examples
c#anonymous-objects

How do I add a List to an anonymous object in C#?


Let's say I have a list:

var list = new List<int>{1,2,3};

How do I add this to an anonymous object such that the object would look like:

{ list : [1,2,3] }

Solution

  • By creating the anonymous object:

    var anon = new { list }
    

    I assume that by saying you want it to look like this:

    { list : [1,2,3] }
    

    you are talking about the output of a JSON serialization, in which it would look like once you serialize.