70

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.

Nic
  • 12,220
  • 20
  • 77
  • 105
Yevhen
  • 1,897
  • 2
  • 14
  • 25
  • 1
    By now I think it's safe to change the question title from "C# multidimensional array" to something more appropriate involving "array of anonymous objects" or some such. – Zano Jan 09 '10 at 22:40

6 Answers6

105

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.

Community
  • 1
  • 1
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • 5
    Correct answer like others. +1 for encouraging proper formatting. – Jesper Fyhr Knudsen Jan 09 '10 at 21:34
  • 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. – Noon Silk Jan 10 '10 at 09:52
48

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

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
5

Looks like an array of anonymous types.

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

Kris Krause
  • 7,304
  • 2
  • 23
  • 26
  • 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 }}; – Mikhail Vitik Apr 08 '21 at 11:06
3

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)

Michael Stum
  • 177,530
  • 117
  • 400
  • 535
2

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 }
anq
  • 3,102
  • 21
  • 16
1

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;
}
Nic
  • 12,220
  • 20
  • 77
  • 105
Yevhen
  • 1,897
  • 2
  • 14
  • 25