.net - How to delete an element from an array in C# -


Let's say I have this array,

  int [] number = {1 , 3, 4, 9, 2};  

How do I delete an element from "name"? , Says 4 numbers?

Even the help to remove ArrayList did not help?

  string strNumbers = "1, 3, 4, 9, 2"; ArrayList Numbers = New Arraylight (Strombus Split (Innovation [] {','})); Numbers.RemoveAt (numbers.IndexOf (4)); Forchach (numbers nA) (Response. V (n);}  

If you want to remove all instances without 4, you need to know the index:

LINQ: (.NET Framework 3.5)

  int [] number = {1, 3, 4, 9, 2}; Int numToRemove = 4; numbers = numbers. Where (val = & gt ; Val! = NumToRemove) .Oir ();  

Non-LINQ: (.NET Framework 2.0)

  is not a constant bull notifier (int n) {return n! = 4;} int [] number = {1, 3, 4, 9, 2}; numbers = array All the numbers (numbers, notforts) .Oere ();  

If you just want to remove the first example:

LINQ: (.NET Framework 3.5)

  int [] number = {1, 3, 4, 9 , 2, 4}; Int numToRemove = 4; Int numIndex = Array.IndexOf (number, numToRemove); number = numbers Where ((Val, IDx) => idx! = NumIndex) .Oir ();  

non-LINQ: (.NET Framework 2.0)

  int [] number = {1, 3, 4, 9, 2, 4}; Int numToRemove = 4; Int numIdx = Array.IndexOf (number, numToRemove); & Lt; Integer & gt; Tmp = new list & lt; Int & gt; (Number); Tmp.RemoveAt (numIdx); Numbers = TMP. ToArray ();  

Edit: In the case only you have not already applied it, as Malthist said, aiming at you Requires LINQ code for examples to work on .NET Framework 3.5. If you are targeting 2.0 then you need to refer to non-LINQ examples.


Comments