Click here to Skip to main content
15,889,429 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Misaligned elephants Pin
rbocquier24-Jul-13 0:39
rbocquier24-Jul-13 0:39 
GeneralRe: Misaligned elephants Pin
Matthew Faithfull24-Jul-13 0:53
Matthew Faithfull24-Jul-13 0:53 
GeneralRe: Misaligned elephants Pin
Stuart Dootson24-Jul-13 5:18
professionalStuart Dootson24-Jul-13 5:18 
GeneralRe: Misaligned elephants Pin
Matthew Faithfull24-Jul-13 5:24
Matthew Faithfull24-Jul-13 5:24 
GeneralRe: Misaligned elephants Pin
jschell24-Jul-13 9:26
jschell24-Jul-13 9:26 
GeneralMessage Automatically Removed Pin
Brisingr Aerowing19-Jul-13 6:46
professionalBrisingr Aerowing19-Jul-13 6:46 
GeneralRe: Extension Methods Pin
Andrew Rissing19-Jul-13 7:06
Andrew Rissing19-Jul-13 7:06 
GeneralRe: Extension Methods Pin
Richard Deeming19-Jul-13 7:20
mveRichard Deeming19-Jul-13 7:20 
With any decent implementation of ICollection<T> your method will produce an InvalidOperationException with the message: "Collection was modified; enumeration operation may not execute."

You'll need to store the items to remove in a separate list, and remove them outside of any enumeration of the source list:
C#
public static void RemoveAllBut<T>(this ICollection<T> source, params T[] notRemove)
{
   var itemsToRemove = source.Except(notRemove).ToList();
   itemsToRemove.ForEach(item => source.Remove(item));
}

As Andrew pointed out, your non-generic version won't work. It would need to be:
C#
public static void RemoveAllBut(this IList source, params object[] notRemove)
{
   var itemsToRemove = source.Cast<object>().Except(notRemove).ToList();
   itemsToRemove.ForEach(source.Remove);
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Extension Methods Pin
Gary Wheeler19-Jul-13 7:21
Gary Wheeler19-Jul-13 7:21 
GeneralRe: Extension Methods Pin
TnTinMn19-Jul-13 8:00
TnTinMn19-Jul-13 8:00 
GeneralRe: Extension Methods Pin
Lutosław19-Jul-13 8:08
Lutosław19-Jul-13 8:08 
GeneralRe: Extension Methods Pin
Richard Deeming19-Jul-13 8:55
mveRichard Deeming19-Jul-13 8:55 
GeneralRe: Extension Methods Pin
Lutosław19-Jul-13 9:06
Lutosław19-Jul-13 9:06 
GeneralRe: Extension Methods Pin
PIEBALDconsult19-Jul-13 8:09
mvePIEBALDconsult19-Jul-13 8:09 
GeneralRe: Message Automatically Removed Pin
lewax0019-Jul-13 9:35
lewax0019-Jul-13 9:35 
GeneralRe: Message Automatically Removed Pin
ZurdoDev19-Jul-13 10:22
professionalZurdoDev19-Jul-13 10:22 
GeneralRe: Message Automatically Removed Pin
lewax0021-Jul-13 11:03
lewax0021-Jul-13 11:03 
GeneralRe: Message Automatically Removed Pin
pkfox29-Jul-13 22:26
professionalpkfox29-Jul-13 22:26 
GeneralThis one is for the Java experts Pin
Dennis_E18-Jul-13 0:56
professionalDennis_E18-Jul-13 0:56 
GeneralRe: This one is for the Java experts Pin
Nagy Vilmos18-Jul-13 1:52
professionalNagy Vilmos18-Jul-13 1:52 
GeneralRe: This one is for the Java experts Pin
Fredrik Bornander18-Jul-13 2:10
professionalFredrik Bornander18-Jul-13 2:10 
GeneralRe: This one is for the Java experts Pin
Dennis_E18-Jul-13 2:27
professionalDennis_E18-Jul-13 2:27 
GeneralRe: This one is for the Java experts Pin
Nagy Vilmos18-Jul-13 3:54
professionalNagy Vilmos18-Jul-13 3:54 
GeneralRe: This one is for the Java experts Pin
svella23-Jul-13 3:57
svella23-Jul-13 3:57 
GeneralRe: This one is for the Java experts Pin
Brisingr Aerowing18-Jul-13 6:40
professionalBrisingr Aerowing18-Jul-13 6:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.