C# and arrays of anonymous objects

69,566

Solution 1

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

Solution 2

First, let's reformat that code 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 3

Looks like an array of anonymous types.

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

Solution 4

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

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 }
Share:
69,566
Yevhen
Author by

Yevhen

Gamedev is lifestyle and hobby.

Updated on August 28, 2020

Comments

  • Yevhen
    Yevhen almost 4 years

    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.

  • Jesper Fyhr Knudsen
    Jesper Fyhr Knudsen over 14 years
    Correct answer like others. +1 for encouraging proper formatting.
  • Noon Silk
    Noon Silk over 14 years
    You, sir, use the formatting of the kings. (Well, except that you need spaces between the ='s signs for the Lin word). Nevertheless, I can forgive that as an accident. Proper formatting greatly reduces errors and increases trivially readability.
  • Mikhail Vitik
    Mikhail Vitik about 3 years
    Quote from the article: You can create an array of anonymously typed elements by combining an implicitly typed local variable and an implicitly typed array, as shown in the following example. var anonArray = new[] { new { name = "apple", diam = 4 }, new { name = "grape", diam = 1 }};