Decrement all int values in Dictionary?

Your existing code is an entirely appropriate way to decrement all of the values in a dictionary If you wanted to create a new dictionary, you could use LINQ: EPCs = EPCs. ToDictionary(p => p. Key, p => p.

Value - 1) This will, however, create an entirely new Dictionary.

Your existing code is an entirely appropriate way to decrement all of the values in a dictionary. If you wanted to create a new dictionary, you could use LINQ: EPCs = EPCs. ToDictionary(p => p.

Key, p => p. Value - 1); This will, however, create an entirely new Dictionary instance, rather than modifying the existing instance in place. However, since you tagged your question with linq, I figured I would offer the one way (that I'm aware of) where LINQ could solve your problem.

LINQ operations work on immutable types, so you're correct that there is no way to do it with LINQ without creating a new dictionary. – BlueRaja - Danny Pflughoeft May 10 '10 at 15:15 1 @BlueRaja: I think it's more correct to say that LINQ operations are not designed to modify existing enumerables. There's nothing in LINQ that makes it work on immutable types any differently than it does on mutable types.

– Adam Robinson May 10 '10 at 15:22.

I think this is completely appropriate. But since you asked the question, what are you concerned about that may not be reasonable about this kind of code? You should realize that you have two options to do what you're looking for, either: Modify the existing dictionary by visiting each entry (which your code does), or Create a new dictionary with the computed values you want.

You can do the second easily with LINQ: var newDict = myDict. ToDictionary( kvp => kvp. Key, kvp => kvp.

Value-1 ).

– Adam Robinson May 10 '10 at 15:35 I have removed it as when I ran the code I got an exception about the collection being modified therefore I guess my code is not correct as both answers indicate. – Jon May 11 '10 at 7:46.

This is not a direct answer, but instead of decrementing it for each item you could just store an offset and decrement it on the fly when getting an item, either as specialized class or just in the code in general.

And the runtime complexity award goes to... ;-) – efi May 11 '10 at 7:47.

You can write a little for-each enumerator yourself that takes an action and executes it on every element: public static void ForEach(this IEnumerable source, Action action) { foreach (T element in source) { action(element); } } Use it like this: EPCs. ForEach(x => EPCsx. Key = x.

Value -1); It's not exactly any cleaner than what you had before but a little more compact though. The Reactive Extensions have a similar operator in System. Interactive called Do.

1 KeyValuePair is a read-only type. You cannot modify the contents in this way. – Adam Robinson May 10 '10 at 15:12 That won't work.

The Value property of KeyValuePair does not have a setter - you cannot assign to dictionaries in this way. – LBushkin May 10 '10 at 15:12 Yes, fixed it. I didn't exactly copy-paste his code and thus missed it ;-) – Johannes Rudolph May 10 '10 at 15:14 I've never understood the desire to use the ForEach function on List, and I think there's a reason it was not included in the Enumerable class.

How is this that much less verbose than a standard foreach loop like he has? Simply because you're eliminating the words var and in? This is otherwise identical, other than the fact that you have to use a lambda rather than a normal (faster) sequental set of instructions...? – Adam Robinson May 10 '10 at 15:24 It is identical and no, it's not significantly slower.

The lambda will compile into an anonymous function that is called for every element. Having a ForEach extension is useful when composing enumerable operators for introducing side effects. The OP tagged his question linq and I therefore think he's searching for a linq-style solution.

The suggested ForEach operator statisfies this. – Johannes Rudolph May 10 '10 at 15:29.

Your code is perfectly fine given the circumstances (Dictionary). If you need high performance using something else than a dictionary might be a better choice in the long run.

I have a Dictionary and I simply want to decrement the value in my dictionary by one. I have this but not sure if its best practice. UPDATE: The reason I am trying to decrement the value is becase the value is a index number relating to a position.

When I remove something from the dictionary I then have to decrement that index number in the dictionary. There may be a better way.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions