Generics only makes sense if you really have different type for the list element, and all those types have a common (inherited) member to be used to to filter out the objects to be removed (or provide any common functionality at all). Then you would use generic type
constraints to identify this common type. As you did not specify it, the question does not make sense. However, the idea to use generics may make sense, if you do what I mentioned above.
Besides, using string parameter in your delete method is not just silly, it's a crime (against yourself). You always compare integer property/field, so it should be integer parameter. And, if you parse a string to integer (or anything), uses
int.Parse
, not
Convert
. Finally, your code samples could not compile, because C# is case-sensitive. Why posting not real code?
Let me illustrate the idea on a simple example, which may not match your case:
interface ISno
{
int Sno { get; set; }
}
class Customer : ISno
{
public int Sno { get; set; }
}
static class Test
{
internal static void SelectiveDeleteSno<T>(List<T> list, int snoToMatch)
where T : ISno
{
list.RemoveAll(a => a.Sno == snoToMatch);
}
}
So, if you have anything to specify the constraint, you can do it. If you did not have filtering and had to do something with some collection without using any knowledge on the elements, it could be any non-specified element type, so generic constraint would not be needed.
—SA