The concept of a set is not mentioned, so it is easier to store objects and facilitate value transfer.
Create a car class:
class Car{public string color { get; set; } public string name { get; set; }}
P.s: simple and quick to create car attributes (with the get/Set Method ):
After you enter prop and tab, the public int myproperty {Get; set;} is automatically generated. Click continue tab, modify int, and modify myproperty in tab.
------------------------------------------------------
Create a list set:
List <car> mylist = new list <car> () {new car () {color = "red", name = "Red Flag"}, new car () {color = "green", name = "Hulk "}};
This method is also a fast and simple creation method, which consumes less resources and does not need to create two car object variables for reference.
The common method is as follows:
List <car> mylist = new list <car> (); car car1 = new car (); car1.color = "red"; car. name = ""; car car2 = new car (); car2.color = "green"; car. name = "Hulk"; mylist. add (car1); mylist. add (car2 );
------------------------------------------------
Foreach () loop:
The foreach () loop is basically the same as the for () loop, but foreach () is used to process the iteration of the Set element.
string mycars = ""; foreach(Car car in myList) { mycars += car.name + "--" + car.color + Environment.NewLine; } mytextblock.Text = mycars;
Print result:
Red Flag-red
Hulk -- green