How to Quickly Remove Items From a List
List isn’t an efficient data structure when it comes to removal. You would do better to use a double linked list (LinkedList) as removal simply requires reference updates in the adjacent entries.
List isn’t an efficient data structure when it comes to removal. You would do better to use a double linked list (LinkedList) as removal simply requires reference updates in the adjacent entries.
There is an actual Data Type called KeyValuePair, use like this KeyValuePair<string, string> myKeyValuePair = new KeyValuePair<string,string>(“defaultkey”, “defaultvalue”);
Dictionary<string, string> is a more modern approach. It implements IEnumerable<T> and it’s more suited for LINQy stuff. StringDictionary is the old school way. It was there before generics days. I would use it only when interfacing with legacy code.
Capacity does not equal size. The size parameter that you are passing in simply allocates enough memory for the size. It does not actually define elements. It’s actually kind of a silly requirement of Collections.copy, but it is one nonetheless. The key part from the Collections.copy JavaDocs: The destination list must be at least as … Read more
Using interfaces over concrete types is the key for good encapsulation and for loose coupling your code. It’s even a good idea to follow this practice when writing your own APIs. If you do, you’ll find later that it’s easier to add unit tests to your code (using Mocking techniques), and to change the underlying … Read more
You can flatten the collection with Linq, but it’s still a foreach loop but now more implicit. var items = nvc.AllKeys.SelectMany(nvc.GetValues, (k, v) => new {key = k, value = v}); foreach (var item in items) Console.WriteLine(“{0} {1}”, item.key, item.value); The first line, converts the nested collection to a (non-nested) collection of anonymous objects with … Read more
If RoleAssignments is a List<T> you can use the following code. workSpace.RoleAssignments.RemoveAll(x =>x.Member.Name == shortName);
Using Java8: stringList.stream().map(Integer::parseInt).collect(Collectors.toList());
You are removing the item during a foreach, yes? Simply, you can’t. There are a few common options here: use List<T> and RemoveAll with a predicate iterate backwards by index, removing matching items for(int i = list.Count – 1; i >= 0; i–) { if({some test}) list.RemoveAt(i); } use foreach, and put matching items into … Read more
Then presumably you’ll need to lift them to the non-nullable form, using Nullable.GetUnderlyingType, and perhaps change a few null values to DbNull.Value… Change the assignment to be: row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; and when adding the columns to be: table.Columns.Add(prop.Name, Nullable.GetUnderlyingType( prop.PropertyType) ?? prop.PropertyType); And it works. (?? is the null-coalescing operator; it uses the … Read more