views:

345

answers:

6

what does such 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 such array manually to suite this DataSource thnx in advance.

+9  A: 

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

Mark Byers
+2  A: 

Looks like an array of anonymous types.

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

Kris Krause
+8  A: 

First, let's reformat that code a little: (Note: this was written before the original question was reformatted.)

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 datasource: you seem to be doing exactly that with the above code.

stakx
Correct answer like others. +1 for encouraging proper formatting.
Arkain
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
Thank you, kind sir. :) Yes, I forgot the spaces around the =. They're added now.
stakx
+2  A: 

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
A: 
TGadfly
+1  A: 

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