Click here to Skip to main content
15,897,371 members
Home / Discussions / C#
   

C#

 
GeneralRe: find an item in list collection to add or update Pin
arkiboys21-Feb-11 5:07
arkiboys21-Feb-11 5:07 
GeneralRe: find an item in list collection to add or update Pin
_Erik_21-Feb-11 5:15
_Erik_21-Feb-11 5:15 
GeneralRe: find an item in list collection to add or update Pin
Luc Pattyn21-Feb-11 5:09
sitebuilderLuc Pattyn21-Feb-11 5:09 
GeneralRe: find an item in list collection to add or update Pin
_Erik_21-Feb-11 5:14
_Erik_21-Feb-11 5:14 
AnswerRe: find an item in list collection to add or update Pin
Luc Pattyn21-Feb-11 5:11
sitebuilderLuc Pattyn21-Feb-11 5:11 
AnswerRe: find an item in list collection to add or update Pin
#realJSOP21-Feb-11 6:03
professional#realJSOP21-Feb-11 6:03 
GeneralRe: find an item in list collection to add or update Pin
Orcun Iyigun21-Feb-11 13:37
Orcun Iyigun21-Feb-11 13:37 
GeneralRe: find an item in list collection to add or update Pin
Pete O'Hanlon22-Feb-11 1:36
mvePete O'Hanlon22-Feb-11 1:36 
Sorry John, but this isn't a great solution if the size of the data gets too large. Basically, every time you add a record you increase the amount of time it takes to search for a new item just to see if it can be added. I recently had to rework some code that somebody had put together like this to find and add files into a list of monitored files. As you can imagine, it works fine when there are only a few items in the list, but the algorithm completely falls apart when you start getting to the few tens of thousands.

A simpler way is to use a Dictionary. As in:
C#
private Dictionary<string, MyItem> _items = new Dictionary<string, MyItem>();

public void Add(MyItem value)
{
  if (!_items.ContainsKey(value.MyProperty))
  {
    _items.Add(value.MyProperty, value);
  }
}
Alternatively if the OP is using .NET 3.5+, a HashSet provides a good choice because it removes the duplication where the dictionary contains MyProperty both in the value, and in the key.

I'm not a stalker, I just know things. Oh by the way, you're out of milk.

Forgive your enemies - it messes with their heads


My blog | My articles | MoXAML PowerToys | Onyx


GeneralRe: find an item in list collection to add or update Pin
#realJSOP22-Feb-11 3:31
professional#realJSOP22-Feb-11 3:31 
QuestionAT commands in c# Pin
aeman21-Feb-11 3:30
aeman21-Feb-11 3:30 
AnswerRe: AT commands in c# Pin
Thomas Krojer21-Feb-11 5:40
Thomas Krojer21-Feb-11 5:40 
QuestionSetting scanner device property constants (WIA) Pin
Etienne_12320-Feb-11 23:57
Etienne_12320-Feb-11 23:57 
QuestionRS232 interfacing help Pin
wjbjnr20-Feb-11 23:56
wjbjnr20-Feb-11 23:56 
AnswerRe: RS232 interfacing help Pin
OriginalGriff21-Feb-11 0:05
mveOriginalGriff21-Feb-11 0:05 
AnswerRe: RS232 interfacing help Pin
Dalek Dave21-Feb-11 0:21
professionalDalek Dave21-Feb-11 0:21 
GeneralRe: RS232 interfacing help Pin
OriginalGriff21-Feb-11 0:54
mveOriginalGriff21-Feb-11 0:54 
GeneralRe: RS232 interfacing help Pin
wjbjnr21-Feb-11 10:50
wjbjnr21-Feb-11 10:50 
GeneralRe: RS232 interfacing help Pin
OriginalGriff21-Feb-11 21:51
mveOriginalGriff21-Feb-11 21:51 
Questionbuild a Dynamic code - c# 4.0 Pin
arkiboys20-Feb-11 23:17
arkiboys20-Feb-11 23:17 
AnswerRe: build a Dynamic code - c# 4.0 Pin
Thomas Krojer20-Feb-11 23:34
Thomas Krojer20-Feb-11 23:34 
AnswerRe: build a Dynamic code - c# 4.0 Pin
dasblinkenlight20-Feb-11 23:37
dasblinkenlight20-Feb-11 23:37 
AnswerRe: build a Dynamic code - c# 4.0 Pin
RobCroll21-Feb-11 2:40
RobCroll21-Feb-11 2:40 
GeneralRe: build a Dynamic code - c# 4.0 Pin
arkiboys21-Feb-11 4:42
arkiboys21-Feb-11 4:42 
QuestionStatic or non-static method performance and thread safety? Pin
Chesnokov Yuriy20-Feb-11 21:54
professionalChesnokov Yuriy20-Feb-11 21:54 
AnswerRe: Static or non-static method performance and thread safety? Pin
dasblinkenlight20-Feb-11 23:50
dasblinkenlight20-Feb-11 23:50 

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.