Remove specific elements from ordinary arrays
Solution:
In order to overcome this limit, you can first convert the ordinary array to List, perform the removal operation, and then convert the modified List to the array, so as to use the List's remaint () method.
Extension method replacement scheme:
<code class="language-c#">var foos = new List<foo>(array); foos.RemoveAt(index); return foos.ToArray();</code>
or, you can consider using an expansion method of using an analog ordinary array Removeat () function:
This extension method allows more conveniently to remove operations:
<code class="language-c#">public static T[] RemoveAt<T>(this T[] source, int index) { T[] dest = new T[source.Length - 1]; if( index > 0 ) Array.Copy(source, 0, dest, 0, index); if( index < source.Length -1 ) Array.Copy(source, index + 1, dest, index, source.Length - index - 1); return dest; }</code>
The above is the detailed content of How to Remove an Element from a C# Array?. For more information, please follow other related articles on the PHP Chinese website!