Hi guys
How to add and remove elements in array ? its kinda hard I wanna know the basic one.
Arr = new string {“love”,“hate”,“girl”,“head”,“to”,“me”};
here’s my array
Hi guys
How to add and remove elements in array ? its kinda hard I wanna know the basic one.
Arr = new string {“love”,“hate”,“girl”,“head”,“to”,“me”};
here’s my array
in C# you cannot mutate an array like this. If you want a dynamic array I suggest looking at 1
var someList = new List<string>(new[] { "love","hate","girl","head","to","me" });
// the list is now { "love","hate","girl","head","to","me","another" }
someList.Add("another");
// the list is now { "love","girl","head","to","me","another" }
someList.RemoveAt(1);
You can also use an ArrayList if all elements will be of the same type (i.e. string).
ArrayList arr = new ArrayList();
arr.AddRange(new string[] { "love", "hate", "girl", "head", "to", "me" });
// print the contents of the ArrayList
names;
// adding another element to the ArrayList
names.Add("boy");
// removing an element from the ArrayList
names.Remove("head");
Both ArrayList and Generic Lists are collections; however, it is better to use a Generic List as it performs better and can contain mixed types.