C# and arrays of anonymous objects

C#asp.net.NetArraysAnonymous Types

C# Problem Overview


What does such an expression mean?

obj.DataSource = new[]
{
    new {Text = "Silverlight", Count = 10, Link="/Tags/Silverlight" },
    new {Text = "IIS 7", Count = 11, Link="http://iis.net" },
    new {Text = "IE 8", Count = 12, Link="/Tags/IE8" },
    new {Text = "C#", Count = 13, Link="/Tags/C#" },
    new {Text = "Azure", Count = 13, Link="?Tag=Azure" }
};

Especially these lines: new {Text = "IIS 7"... }

How can I create an array like this manually to suit this DataSource.

C# Solutions


Solution 1 - C#

First, let's reformat [that code](https://stackoverflow.com/revisions/2035017/1 "(This answer was written before the question was edited. See the original code by following this link.)") a little:

obj.DataSource = new[]
{
    new {  Text = "Silverlight",  Count = 10,  Link = "/Tags/Silverlight"  },
    new {  Text = "IIS 7",        Count = 11,  Link = "http://iis.net"     }, 
    new {  Text = "IE 8",         Count = 12,  Link = "/Tags/IE8"          }, 
    new {  Text = "C#",           Count = 13,  Link = "/Tags/C#"           },
    new {  Text = "Azure",        Count = 13,  Link = "?Tag=Azure"         } 
};

This does not look like a multi-dimensional array, but rather like an array of 5 objects. These objects inside the array are of an anonymous type, created and initialized using new { ... }.

Concerning your question how you can manually create such an array to suit the data source: you seem to be doing exactly that with the above code.

Solution 2 - C#

That's not a multidimensional array. That's an array of objects which have been created using object initializers with anonymous types.

Solution 3 - C#

Looks like an array of anonymous types.

http://msdn.microsoft.com/en-us/library/bb397696.aspx

Solution 4 - C#

Just to add: Anonymous types are converted by the compiler to a real object. So the code will be changed to something equivalent of this (MUCH simplified, only to show that the compiler will create an actual class):

internal sealed class AnonymousClass1
{
    internal string Text { get; set; }
    internal int Count { get; set; }
    internal string Link { get; set; }
}

And your code will then be changed to:

obj.DataSource = new AnonymousClass1[]
{
    new AnonymousClass1 {Text = "Silverlight", Count = 10, Link="/Tags/Silverlight" },
    new AnonymousClass1 {Text = "IIS 7", Count = 11, Link="http://iis.net" },
    new AnonymousClass1 {Text = "IE 8", Count = 12, Link="/Tags/IE8" },
    new AnonymousClass1 {Text = "C#", Count = 13, Link="/Tags/C#" },
    new AnonymousClass1 {Text = "Azure", Count = 13, Link="?Tag=Azure" }
};

In one of my programs, I have code like this (simplified!):

var myObjects = new []{
    new { Id = Guid.NewGuid(), Title = "Some Title", Description = string.Empty },
    new { Id = Guid.NewGuid(), Title = "Another Title", Description = string.Empty },
    new { Id = Guid.NewGuid(), Title = "Number 3", Description = "Better than No2, but not much" }
}

foreach(var myObject in myObjects) {
    DoSomeThingWith(myObject.Title);
}

This works because it is just another class (I even get IntelliSense) behind the scenes. The benefit is obviously that I just saved myself from creating a class for this object. In my example, all objects need to look the same as well. (Obviously, doing this for any public members would be a bad idea as the compiler might change the name of the anonymous class if you add/remove some)

Solution 5 - C#

It's making a new object array, containing a group of anonymous objects.

new {Text = "Azure", Count = 13, Link="?Tag=Azure" }

is not creating a hash like similar syntax in say php would, but rather real a real object with the properties Test, Count, and Link set.

A good primer for anonymous objects can be found here

You should be able to use the same syntax to create new structures like this, the property values do not have to be constants:

string text = "Azure";
int count = 13;
string link =  "?Tag=Azure";
new {Text = text, Count = count, Link=link }

Solution 6 - C#

To return such an array from a function I used:

public object GetAnonymousArray()
{
    var tbl = new List<object>();
    while (___)
    {
        //fill array in cycle
        tbl.Add(new {Text = someString, Count = someInt, Link = link});
    }  
    return tbl;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYevhenView Question on Stackoverflow
Solution 1 - C#stakx - no longer contributingView Answer on Stackoverflow
Solution 2 - C#Mark ByersView Answer on Stackoverflow
Solution 3 - C#Kris KrauseView Answer on Stackoverflow
Solution 4 - C#Michael StumView Answer on Stackoverflow
Solution 5 - C#anqView Answer on Stackoverflow
Solution 6 - C#YevhenView Answer on Stackoverflow