initializing an array of structs



In C# is it possible to initialize the items of an array when the array is
an array of structures?



When I try to initialize the items of the array (as shown below) using the
method I would use in C++, I get the compiler error "Array initializers can
only be used in a variable or field initializer. Try using a new expression
instead.":



Is there a way to do this?

Thanks for any help,

Steve



public struct test

{

public int id;

public string name;

}



public test[] testArray = { {1,"name1"}, {2,"name2"} };


.



Relevant Pages

  • Re: compiler bugs
    ... In a language like C, the behavior of programs that reference ... Not if the unoptimizing compiler initializes all variables to some ... portion of the array in your algorithhm. ... that one actually does not need to initialize all variables (unlike ...
    (comp.compilers)
  • Re: initializing an array of structs
    ... When I try to initialize the items of the array using the ... public string name; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Error 216 (related to SetLength)
    ... It might turn out that you're writing to a part of an array that doesn't exist. ... suggests you're talking about an access violation (in which case I'd like to know what addresses the exception reports), but your previous message only quoted the text from a different kind of exception, about changing visibility in OnShow or OnHide. ... I'd even go so far as to argue that OnCreate isn't the right place, ... Since your variables are global, they aren't related to any specific form, so a form isn't what should initialize them. ...
    (alt.comp.lang.borland-delphi)
  • Re: compiler bugs
    ... While I hate disagreeing with you Anton, ... Not if the unoptimizing compiler initializes all variables to some ... portion of the array in your algorithhm. ... being able to not actually initialize the array for the unused ...
    (comp.compilers)
  • Re: behavior-preserving optimization in C, was compiler bugs
    ... initialization is the same with and without optimization. ... initialize to a defined value that also survives, e.g., program ... by zero is in a function that references an uninitialized local array ... changes to register and stack ...
    (comp.compilers)