Click here to Skip to main content
15,921,622 members
Home / Discussions / C#
   

C#

 
QuestionNetwork Programming Pin
Hum Dum15-Nov-10 19:55
Hum Dum15-Nov-10 19:55 
AnswerRe: Network Programming Pin
Jacob D Dixon16-Nov-10 6:54
Jacob D Dixon16-Nov-10 6:54 
GeneralRe: Network Programming Pin
Hum Dum17-Nov-10 21:48
Hum Dum17-Nov-10 21:48 
AnswerRe: Network Programming Pin
RobCroll17-Nov-10 4:23
RobCroll17-Nov-10 4:23 
GeneralRe: Network Programming Pin
Hum Dum17-Nov-10 21:43
Hum Dum17-Nov-10 21:43 
GeneralRe: Network Programming Pin
RobCroll18-Nov-10 20:21
RobCroll18-Nov-10 20:21 
QuestionA Question Of Efficiency Pin
Roger Wright15-Nov-10 17:11
professionalRoger Wright15-Nov-10 17:11 
AnswerRe: A Question Of Efficiency Pin
Luc Pattyn15-Nov-10 17:53
sitebuilderLuc Pattyn15-Nov-10 17:53 
Hi Roger,

there is more for you to explore!

all kinds of collections have a Contains() method, in fact it is part of the IList interface. It typically works by comparing the collection elements one by one with the foreign object, so yes that would slow down for huge collections.

There is one kind of collections that does search more efficiently, it is the Dictionary kind as they use hashing to quickly look up through the key. So if you (1) don't care about the order of the elements in the collection, and (2) have one field in your elements that must be unique, you can simply look that up, like so:

class myItem
    int unique;
    byte[] data;
    string text;
...
}


Dictionary<int, myItem> dict=new Dictionary<int, myItem>();
foreach(myItem mi in incomingItems) {
    if (!dict.ContainsKey(mi.unique)) dict.Add(mi.unique, mi);
}


And then there is HashSet() which is like a list that does not care about duplicates, i.e. it calculates a hash for your elements and it only remembers the youngest one of conflicting hashed entries, so you could do:
HashSet<myItem> set=new HashSet<myItem>();
foreach(myItem mi in incomingItems) {
    set[mi.unique]=mi;
}


with a small chance that different elements happen to have conflicting hash values and all but the latest one of them get lost.

So it all depends on how large your collections grow, whether your items have a unique field, and how well you can accept the odd hash conflict.

Smile | :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.


GeneralRe: A Question Of Efficiency Pin
Roger Wright15-Nov-10 18:15
professionalRoger Wright15-Nov-10 18:15 
GeneralRe: A Question Of Efficiency Pin
Luc Pattyn16-Nov-10 2:27
sitebuilderLuc Pattyn16-Nov-10 2:27 
AnswerRe: A Question Of Efficiency Pin
_Erik_16-Nov-10 1:58
_Erik_16-Nov-10 1:58 
GeneralRe: A Question Of Efficiency Pin
Roger Wright17-Nov-10 14:14
professionalRoger Wright17-Nov-10 14:14 
AnswerRe: A Question Of Efficiency Pin
PIEBALDconsult16-Nov-10 2:17
mvePIEBALDconsult16-Nov-10 2:17 
GeneralRe: A Question Of Efficiency Pin
Roger Wright17-Nov-10 14:11
professionalRoger Wright17-Nov-10 14:11 
GeneralRe: A Question Of Efficiency Pin
PIEBALDconsult18-Nov-10 2:14
mvePIEBALDconsult18-Nov-10 2:14 
AnswerRe: A Question Of Efficiency Pin
RobCroll17-Nov-10 4:12
RobCroll17-Nov-10 4:12 
GeneralRe: A Question Of Efficiency Pin
Roger Wright17-Nov-10 14:10
professionalRoger Wright17-Nov-10 14:10 
QuestionContrast problem Pin
pancakeleh15-Nov-10 16:57
pancakeleh15-Nov-10 16:57 
AnswerRe: Contrast problem Pin
Roger Wright15-Nov-10 17:14
professionalRoger Wright15-Nov-10 17:14 
GeneralRe: Contrast problem Pin
pancakeleh15-Nov-10 17:22
pancakeleh15-Nov-10 17:22 
GeneralRe: Contrast problem Pin
Dr.Walt Fair, PE15-Nov-10 18:03
professionalDr.Walt Fair, PE15-Nov-10 18:03 
GeneralRe: Contrast problem Pin
Roger Wright15-Nov-10 18:24
professionalRoger Wright15-Nov-10 18:24 
AnswerRe: Contrast problem Pin
pancakeleh15-Nov-10 19:05
pancakeleh15-Nov-10 19:05 
GeneralRe: Contrast problem Pin
pancakeleh15-Nov-10 21:34
pancakeleh15-Nov-10 21:34 
GeneralRe: Contrast problem Pin
Caslen16-Nov-10 2:46
Caslen16-Nov-10 2:46 

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.