SyncMaster
SyncMaster

Reputation: 9936

Removing element from ParameterInfo array in C#

I have a ParameterInfo array. I need to remove certain values from the array. How can i do this.?

Consider i havae, ParameterInfo[] pi containing the values,

{Int32 param1}
{System.String param2}
{System.Collections.Hashtable param3}

I need to remove the 2nd values from the array, i.e {System.String param2}. How can i do this.?

Upvotes: 0

Views: 204

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500675

You can't actually remove an element from an array, because arrays are a fixed size. However, you can create a new array which omits the old element:

public static T[] RemoveElement<T>(T[] original, int elementToRemove)
{
    T[] ret = new T[original.Length-1];
    Array.Copy(original, 0, ret, 0, elementToRemove);
    Array.Copy(original, elementToRemove+1, ret, elementToRemove,
               ret.Length - elementToRemove);
    return ret;
}

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062865

You can't remove items from an array. You can, however, create a new array without the item. Probably the most convenient (but not necessarily efficient) way would be via a list:

List<ParameterInfo> list = new List<ParameterInfo>(args);
list.RemoveAt(1);
args = list.ToArray();

Upvotes: 3

Related Questions