我想知道是否可以使用扩展方法从通用列表中删除所有相同种类的对象,就像这段代码一样:
我可以通过使用以下代码来完成此操作:
public static Remove<T>(this List<[any type]> list)
{
// some code to remove the objects of type T from the list
}
我可以通过使用以下代码来完成此操作:
public static Remove<T, K>(this List<K> list)
{
// some code to remove the objects of type T from the List<K>
}
但我希望只使用一个类型(T),而无需指定任何类型K。通过这样做,用户可以通过简单地编写以下内容来使用此扩展方法:
List<object> list = new List<object>();
list.Add(1);
list.Add("text");
// remove all int type objects from the list
list.Remove<int>();
我需要一个扩展方法,能够像上面的代码一样做某件事情,这正是我所需要的。
最好的问候
foreach(var item in toRemove) list.Remove(item); }
} - user354583