Setting list/array to specific value C#

In Python, you can set a static list to a value like this:

my_list = [a, b, c]

I can’t seem to figure out how to do this in C#.
Any ideas? Thanks!

You can try this:

// Declare list
List <string> my_list = new List <string> ();

// Add values to list
my_list.Add(a);
my_list.Add(b);
my_list.Add(c);
1 Like

Ah, okay. So it is impossible to create all values at once?

I have quite a long list so this would be unpractical, :sweat_smile: although I’ll keep that in mind for shorter lists. :slight_smile:

You can initialize the list with values like this:

List<string> my_List = new List<string>()
{
    "Item1","Item2","Item3"
};
1 Like

You could try this

// Declare list
string[] my_list;

// Add values to list
my_list = new string [] {"value_1", "value_2", "value_3"};

But if you want to make your list more dynamic, it’s much preferable to use List.

UPDATE: Is there a specific pattern on your values? You could probably try looping through it and let the loop add it in the list for you.

1 Like

Thank you!! :smile: Works like a charm.

Thanks! No, there isn’t a specific pattern. It is a large index of words.

Awesome.

Maybe this can help. Goodluck on your project!

Thanks for the link! It was quite helpful. :slight_smile: