I have IDID ILID properties and a private string variable that contains a comma separated list, how to gracefully populate an IList collection?
I asked a mentor and I learned a neat way to populate a List with .AddRange (...), but now I realized that I should return an IList property that does not seem to support the .AddRange method.
public IList CategoryIDs { get { return list.Split(',') .ToList<string>() .ConvertAll<int>(new Converter<string, int>(s => int.Parse(s))); } }
// assignment var ids = "1,2,3,4,5"; obj.CategoryIDs = ids.Split (','); // or - if you want "add" capabilities obj.CategoryIDs = new ArrayList (ids.Split (',')); // encapsulation IList CategoryIDs { get { return ids.Split(','); } }
Just create a new list, use the methods you need and return the list. Since List implements IList, it will be valid for this property.
You can also just add a few times:
var IDs = from s in commaSeparatedList.Split(',') select int.Parse(s); foreach(var id in IDs) theIList.Add(id);
Try the following:
public class Class1 { private String categoryIDList = "1,2,3"; public Class1() { List<Int32> categoryList = new List<Int32>(); String[] categoryIDs = categoryIDList.Split(","); foreach(String category in categoryIDs) categoryList.Add(Int32.Parse( category)); } }
Source: https://habr.com/ru/post/1719468/More articles:Best Visual-Studio tool as a Linux development tool - pythonA more complex macro of words that I'm used to (i.e. I can't write it) - vbaHow to sort an array of objects in PHP? - sortingAsp.net: how a session works in a server farm environment - c #Smalltalk Collections - collectionsJson tool for XML? - jsonUsing a previously generated RSA public / private key with .net infrastructure - securityConvert a whole to color, starting with red and cyclic - colorsКак ссылаться на атрибут XML с помощью XPath? - xpathSample code. Why can I access this NSString object after I released it? - objective-cAll Articles