+ 3

Write a c# program which insert and delete the array?

c#

28th Nov 2017, 2:32 PM
sardar nabeel
sardar nabeel - avatar
4 Answers
+ 3
I'm assuming you are working with a basic array of strings: var strItems = new string[] { "1", "2", "3", "4", "5" }; In .NET, that array is always going to be 5 elements long. In order to remove an element, you are going to have to copy the remaining elements to a new array and return it. Setting the value at a position to null does not remove it from the array. Now, with things like LINQ this is very easy (not shown here), or you could cheat using the List<> collection and do this: var list = new List<string>(strItems); list.Remove("3"); strItems = list.ToArray(); But I don't think that's going to teach you anything. The first step is to find the index of the element you wish to remove. You can use Array.IndexOf to help you out. Let's find the middle element, "3": int removeIndex = Array.IndexOf(strItems, "3"); If the element was not found, it will return a -1, so check for that before doing anything. if (removeIndex >= 0) { // continue... } For adding item in list you can use code shown below: list.Add(ā€œitem1ā€); // if type of list is string
28th Nov 2017, 8:27 PM
Jamal Kaksouri
Jamal Kaksouri - avatar
+ 4
thanks now i have removed link
28th Nov 2017, 2:59 PM
sardar nabeel
sardar nabeel - avatar
+ 4
plz answer my que if u know
28th Nov 2017, 3:02 PM
sardar nabeel
sardar nabeel - avatar