Is it possible to add a list to a struct?
public struct test
{
public string x;
list<string> y = new list<string>();
}
something like that?
ive been trying but im just not getting it
Use the AddRange() method to append a second list to an existing list. list1. AddRange(list2);
A list can contain any sort object, even another list (sublist), which in turn can contain sublists themselves, and so on. This is known as nested list. You can use them to arrange data into hierarchical structures.
A struct object can be created with or without the new operator, same as primitive type variables. Above, an object of the Coordinate structure is created using the new keyword.
Python provides a method called . append() that you can use to add items to the end of a given list. This method is widely used either to add a single item to the end of a list or to populate a list using a for loop.
Yes you can have a list in struct but you cannot initialise it with a field initialiser and instead you must use the constructor.
struct MyStruct
{
public List<string> MyList;
public int MyInt;
public MyStruct(int myInt)
{
MyInt = myInt;
MyList = new List<string>();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With