I can create list and use RemoveAll to remove some elements, but I need to do this using interfaces. How to do it? List elements is String.
-
1Welcome to StackOverflow. Please read this carefully: http://stackoverflow.com/help/how-to-ask – Jens Mar 14 '15 at 07:35
-
Please be more specific. Why do you "need to do this using interfaces"? If `RemoveAll()` works for you, why don't you just use that? What code have you tried? What did that code do? How was that different from what you wanted? Please see http://stackoverflow.com/help/mcve and http://stackoverflow.com/help/how-to-ask for advice on how to present your question in a clear, useful way. – Peter Duniho Mar 14 '15 at 07:35
-
This is my homework on programming in university. I need to remove from the list of all the rows with large letters using interfaces. – user3750141 Mar 14 '15 at 07:41
-
See [Why doesn't IList support AddRange](http://stackoverflow.com/questions/11538259/why-doesnt-ilist-support-addrange), the concept is the same. `IList<>` doesn't support many methods. – xanatos Mar 14 '15 at 07:41
-
I'm voting to close this question as off-topic because because it exists other place for homework. – JPBlanc Mar 14 '15 at 08:20
-
When you say "interfaces", do you mean `IList
` instead of `List – CodesInChaos Mar 14 '15 at 08:22`? In that case use [this extension method](http://stackoverflow.com/questions/4086772/list-not-lose-the-reference/4086877#4086877). Or do you need to represent the predicate as an interface? (a dumb idea, but fitting for homework) -
@JPBlanc no, homework is welcome here, but it needs to be an answerable question that is written in a way people can understand. Don't close for being homework, close for needing editing to be an answerable question. – Kate Gregory Apr 06 '15 at 21:33
-
@Kate Gregory homework is a personal work not a community one. Once anyone put a minimum personal reflexion on the top of his homewok exercise, I will no longer detect it as an homework. – JPBlanc Apr 07 '15 at 03:57
2 Answers
Here it is what you asked:
// An interface
public interface IMySelector
{
bool IDontLike(string str);
}
// A class implementing the interface
public class MySelector : IMySelector
{
public bool IDontLike(string str)
{
if (str.StartsWith("foo"))
{
return true;
}
return false;
}
}
List<string> list = new List<string> { "foo1", "foo2", "bar1", "bar2" };
// Using the interface
IMySelector selector = new MySelector();
// Begin from last, it will be faster to remove
for (int i = list.Count - 1; i >= 0; i--)
{
// Your condition
if (selector.IDontLike(list[i]))
{
list.RemoveAt(i);
}
}
There is an interface, a class implementing the interface and the code that uses the interface to select which elements to remove. Note how I remove the elements from the bottom to the top. It's faster and it requires one less row of code :-) (if you had a for [0... list.Count)
you would have if (selector...) { list.RemoveAt(1); i--; }
)
As a small note, in C# often you use delegates instead of single-method interfaces.
With IEquatable<T>
public class MySelector : IEquatable<string>
{
public bool Equals(string str)
{
// Strange concept of equality... All the
// words that start with foo are equal :-)
if (str.StartsWith("foo"))
{
return true;
}
return false;
}
}
List<string> list = new List<string> { "foo1", "foo2", "bar1", "bar2" };
// Using the interface
IEquatable<string> selector = new MySelector();
// Begin from last, it will be faster to remove
for (int i = list.Count - 1; i >= 0; i--)
{
// Your condition
if (selector.Equals(list[i]))
{
list.RemoveAt(i);
}
}

- 109,618
- 12
- 197
- 280
-
In my task you need to inherit from the interface and override the appropriate method. – user3750141 Mar 14 '15 at 07:47
-
@user3750141 Then you should show us your interface and something of your code. "I can create list and use RemoveAll to remove some elements, but I need to do this using interfaces" doesn't mean anything. – xanatos Mar 14 '15 at 07:49
-
I have List
ls = new List – user3750141 Mar 14 '15 at 07:55(); ls.add("dsfsd"); ls.add("DSFSDsd"); I need to remove all elements in list who have uppercase letters. For example ls.Remove(new Compare()), where new Compare() - it's a class, who inherit some interface. -
-
For example https://msdn.microsoft.com/en-us/library/ms131190(v=vs.110).aspx – user3750141 Mar 14 '15 at 08:16
-
@user3750141 There is no standard *interface* for this, because in C# you'd use standard *delegates* for this, either `Predicate
` or `Func – CodesInChaos Mar 14 '15 at 08:24`.
Write an extension method. Call it RemoveAll<TItem>
. You want to extend the interface IList<TItem>
so choose the this
-marked parameter accordingly. Also make a Func<TItem, bool>
parameter. In the body of the method, foreach
, use the delegate instance, and call the IList<>.Remove
instance method.
Since this is homework, I think I provided enough details.

- 60,409
- 11
- 110
- 181