C # initialize static list in class

What I'm trying to get is a global global list initialized with strings. If I only needed a simple list, I could just initialize the list with strings separated by a comma like this

public static readonly List<string> _architecturesName = new List<string>() {"x86","x64" }; 

I set the static Globals class, in this class I add a list based on another ArchitecturesClass class, which will be used as fields for the list, similar to what was done here: Is 2-dimensional lists possible in C #?

  public class ArchecturesClass { public string Id { get; set; } public string Name { get; set; } } `*test1->*` public static readonly List<ArchecturesClass> ArchitectureList = new List<ArchecturesClass>() { "2", "9"}; `*test2->*` public static readonly List<ArchecturesClass> ArchitectureList = new List<ArchecturesClass>() {architecturesId = "2", architecturesName = "3"}; 

The error in the lines is that the collection initialization has some arguments, and in the end I want all the classes in the project to be able to read something like Globals.ArchtecutreList.ID and map Globals.ArchtecutreList.Name; , and I would like to initialize this in a global class without being in a method.

+7
c #
source share
1 answer

Syntax

 new List<ArchecturesClass>() {architecturesId = "2", architecturesName = "3"}; 

it should be

 new List<ArchecturesClass>() { new ArchecturesClass>() { architecturesId = "2", architecturesName = "3"}}; 

Collection initializers expect you to provide instances of the type contained in your list.

Another attempt

 public static readonly List<ArchecturesClass> ArchitectureList = new List<ArchecturesClass>() { "2", "9"}; 

fails because "2" and "9" are strings, not instances of ArchitecturesClass .

+12
source share

All Articles