Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to add a list to a structure?

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

like image 201
Crash893 Avatar asked May 13 '09 04:05

Crash893


People also ask

Can you add a list to a list C #?

Use the AddRange() method to append a second list to an existing list. list1. AddRange(list2);

Can we create a list inside another list?

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.

Can we create object of structure?

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.

How to fill up a list in Python?

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.


1 Answers

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>();
    }
}
like image 199
sipsorcery Avatar answered Sep 25 '22 03:09

sipsorcery