Click here to Skip to main content
15,943,638 members

Comments by Matt T Heffron (Top 200 by date)

Matt T Heffron 12-Apr-24 19:59pm View    
Yes, it's as useless of a problem now as it was when given to young Carl Friedrich Gauss. His solution would, of course, be the "right" way to compute that number.
Matt T Heffron 12-Apr-24 19:38pm View    
Yes it will, because the list is List<object>. That means that the list can contain ANY combination of types of items.
The list is created ONLY when the first item with a given key is added to uniqueComp. After that any additional items with the same key will be added to the existing list that is associated with that key.
IF you wish to RESTRICT the list associated with a key to contain ONLY items of the type of the first item added with a specific key, then that's a whole different problem.
Matt T Heffron 2-Feb-22 18:27pm View    
This is not an issue of "optimizing" the code, but of debugging.
They are very different things and should never be confused!
"Optimizing" code that isn't working is, in my 40+ years of experience, ALWAYS the wrong thing to do!

"Classic" advice (but still quite appropriate)
The First Rule of Program Optimization:
Don’t do it.
The Second Rule of Program Optimization (for experts only!):
Don’t do it yet.
— Michael Jackson
Matt T Heffron 2-Feb-22 18:12pm View    
Also, your Stack(IEnumerable<t> collection) constructor, counts the items in the collection, and allocates the myList, but never puts them into myList!
Matt T Heffron 2-Feb-22 18:01pm View    
Your Pop() method never modifies your myList, so if you call it a second time it will return the same item again. You should not use a separate myStack. Just use myList.