Click here to Skip to main content
15,891,005 members
Home / Discussions / C#
   

C#

 
GeneralRe: Avoid contextmenustrip pop-up Pin
cocoonwls14-May-08 15:09
cocoonwls14-May-08 15:09 
GeneralRe: Avoid contextmenustrip pop-up Pin
baranils14-May-08 18:59
baranils14-May-08 18:59 
GeneralRe: Avoid contextmenustrip pop-up Pin
cocoonwls14-May-08 22:14
cocoonwls14-May-08 22:14 
QuestionProblem with C# 3.0 Collection and Object Initializers [modified] Pin
User 665813-May-08 7:58
User 665813-May-08 7:58 
AnswerRe: Problem with C# 3.0 Collection and Object Initializers Pin
Gareth H13-May-08 9:06
Gareth H13-May-08 9:06 
GeneralRe: Problem with C# 3.0 Collection and Object Initializers Pin
User 665813-May-08 9:13
User 665813-May-08 9:13 
GeneralRe: Problem with C# 3.0 Collection and Object Initializers Pin
Gareth H13-May-08 9:36
Gareth H13-May-08 9:36 
AnswerRe: Problem with C# 3.0 Collection and Object Initializers Pin
Ed.Poore13-May-08 10:20
Ed.Poore13-May-08 10:20 
He's got himself all confused to put it bluntly.  You can initialise collections using the new expressions, however you have to do as Gareth H pointed out, i.e.:
var customers = new List<Customer>()
{
    new Customer() { Id = 0, Name = "Dave", City = "Sarasota" },
    // ...
};
Where he's got mixed up is with the anonymous types, you could if you don't need to use the customers outside your existing method do the following:
var customers = new[]
{
    new { Id = 0, Name = "Dave", City = "Sarasota" },
    new { Id = 1, Name = "Ed", City = "London" },
};
foreach (var customer in customers)
{
    Console.WriteLine("{0} [{1}]", customer.Name, customer.City);
}
Which means you don't have to explicitly create a class to hold the data.  However as I mentioned you cannot pass this data (directly) to another class or even function within the same class.  What the compiler does is generate a "random" new class automatically (something like <>A_$72832$ for example, which is illegal in anything but IL).

But you can't mix the two, although it would save a little typing.


I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

GeneralRe: Problem with C# 3.0 Collection and Object Initializers Pin
User 665813-May-08 10:39
User 665813-May-08 10:39 
GeneralRe: Problem with C# 3.0 Collection and Object Initializers Pin
Ed.Poore13-May-08 11:14
Ed.Poore13-May-08 11:14 
QuestionHelp can't find how retrieve element in a list!! Pin
papy-boom13-May-08 5:34
papy-boom13-May-08 5:34 
AnswerRe: Help can't find how retrieve element in a list!! Pin
User 665813-May-08 7:41
User 665813-May-08 7:41 
AnswerRe: Help can't find how retrieve element in a list!! Pin
Christian Graus13-May-08 13:22
protectorChristian Graus13-May-08 13:22 
GeneralRe: Help can't find how retrieve element in a list!! Pin
papy-boom13-May-08 22:35
papy-boom13-May-08 22:35 
QuestionEnsure a parameter is not null Pin
-Dy13-May-08 5:20
-Dy13-May-08 5:20 
GeneralRe: Ensure a parameter is not null Pin
CPallini13-May-08 5:43
mveCPallini13-May-08 5:43 
AnswerRe: Ensure a parameter is not null Pin
User 665813-May-08 7:25
User 665813-May-08 7:25 
AnswerRe: Ensure a parameter is not null Pin
Dave Sexton13-May-08 10:43
Dave Sexton13-May-08 10:43 
GeneralRe: Ensure a parameter is not null Pin
Vikram A Punathambekar13-May-08 19:06
Vikram A Punathambekar13-May-08 19:06 
AnswerRe: Ensure a parameter is not null Pin
Christian Graus13-May-08 11:45
protectorChristian Graus13-May-08 11:45 
GeneralRe: Ensure a parameter is not null Pin
Malcolm Smart13-May-08 12:35
Malcolm Smart13-May-08 12:35 
GeneralRe: Ensure a parameter is not null Pin
Christian Graus13-May-08 13:23
protectorChristian Graus13-May-08 13:23 
GeneralRe: Ensure a parameter is not null Pin
S. Senthil Kumar13-May-08 17:36
S. Senthil Kumar13-May-08 17:36 
GeneralRe: Ensure a parameter is not null Pin
Vikram A Punathambekar13-May-08 19:08
Vikram A Punathambekar13-May-08 19:08 
GeneralRe: Ensure a parameter is not null Pin
Malcolm Smart13-May-08 21:24
Malcolm Smart13-May-08 21:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.