How is it possible to initialize (with a C# initializer) a list of strings? I have tried with the example below but it\'s not working.
List opt
Just remove ()
at the end.
List<string> optionList = new List<string>
{ "AdditionalCardPersonAdressType", /* rest of elements */ };
List<string> facts = new List<string>() {
"Coronavirus (COVID-19) is an illness caused by a virus that can spread from personto person.",
"The virus that causes COVID-19 is a new coronavirus that has spread throughout the world. ",
"COVID-19 symptoms can range from mild (or no symptoms) to severe illness",
"Stay home if you are sick,except to get medical care.",
"Avoid public transportation,ride-sharing, or taxis",
"If you need medical attention,call ahead"
};
Your function is just fine but isn't working because you put the ()
after the last }
. If you move the ()
to the top just next to new List<string>()
the error stops.
Sample below:
List<string> optionList = new List<string>()
{
"AdditionalCardPersonAdressType","AutomaticRaiseCreditLimit","CardDeliveryTimeWeekDay"
};
This is how you initialize and also you can use List.Add() in case you want to make it more dynamic.
List<string> optionList = new List<string> {"AdditionalCardPersonAdressType"};
optionList.Add("AutomaticRaiseCreditLimit");
optionList.Add("CardDeliveryTimeWeekDay");
In this way, if you are taking values in from IO, you can add it to a dynamically allocated list.
Move round brackets like this:
var optionList = new List<string>(){"AdditionalCardPersonAdressType","AutomaticRaiseCreditLimit","CardDeliveryTimeWeekDay"};