Click here to Skip to main content
15,887,386 members
Home / Discussions / C#
   

C#

 
AnswerRe: String Problem Pin
KennyPatel17-May-08 5:12
KennyPatel17-May-08 5:12 
AnswerRe: String Problem Pin
Ravi Bhavnani17-May-08 8:08
professionalRavi Bhavnani17-May-08 8:08 
QuestionWMI Help Pin
StevenWalsh17-May-08 4:48
StevenWalsh17-May-08 4:48 
AnswerRe: WMI Help Pin
Mircea Puiu17-May-08 21:15
Mircea Puiu17-May-08 21:15 
QuestionCorp a selection from Image? Pin
jamilkhan00717-May-08 4:47
jamilkhan00717-May-08 4:47 
AnswerRe: Corp a selection from Image? Pin
Anthony Mushrow17-May-08 7:03
professionalAnthony Mushrow17-May-08 7:03 
QuestionAddressbook from Outlook 2003 Pin
daniel9917-May-08 4:40
daniel9917-May-08 4:40 
Questionmy Dictionary removal code correct? Pin
George_George17-May-08 3:25
George_George17-May-08 3:25 
Hello everyone,


I want to iterator a Dictionary variable instance, and remove all elements under a specific condition.

Here is my code. My questions about whether the following solution will function correctly?

1. will dic.Remove(key) operation impact keys collection?
2. will dic.Remove(key) operation impact dic[key] operation?

I have such concern is because I heard while interation in the middle, remove any elements inside the Dictionary is dangerous because it will make some reference invalid and impact result. Any ideas?

Dictionary<string, Foo>.KeyCollection keys = dic.Keys;
foreach (string key in keys)
{
    // order is a public property of Foo class
    if (dic[key].order > 100)
    {
        // will keys collection variable be impacted if we remove here?
        // will dic[key] operation also be impacted?
        dic.Remove(key);
    }
}



thanks in advance,
George
AnswerRe: my Dictionary removal code correct? Pin
PIEBALDconsult17-May-08 4:09
mvePIEBALDconsult17-May-08 4:09 
GeneralRe: my Dictionary removal code correct? Pin
George_George17-May-08 4:14
George_George17-May-08 4:14 
GeneralRe: my Dictionary removal code correct? Pin
PIEBALDconsult17-May-08 5:29
mvePIEBALDconsult17-May-08 5:29 
GeneralRe: my Dictionary removal code correct? Pin
George_George17-May-08 21:15
George_George17-May-08 21:15 
GeneralRe: my Dictionary removal code correct? Pin
PIEBALDconsult18-May-08 5:02
mvePIEBALDconsult18-May-08 5:02 
GeneralRe: my Dictionary removal code correct? Pin
George_George19-May-08 1:02
George_George19-May-08 1:02 
GeneralRe: my Dictionary removal code correct? Pin
PIEBALDconsult19-May-08 12:58
mvePIEBALDconsult19-May-08 12:58 
GeneralRe: my Dictionary removal code correct? Pin
George_George20-May-08 4:02
George_George20-May-08 4:02 
GeneralRe: my Dictionary removal code correct? Pin
PIEBALDconsult20-May-08 14:17
mvePIEBALDconsult20-May-08 14:17 
GeneralRe: my Dictionary removal code correct? Pin
George_George21-May-08 0:35
George_George21-May-08 0:35 
AnswerRe: my Dictionary removal code correct? Pin
Guffa17-May-08 6:21
Guffa17-May-08 6:21 
GeneralRe: my Dictionary removal code correct? Pin
George_George17-May-08 21:12
George_George17-May-08 21:12 
GeneralRe: my Dictionary removal code correct? Pin
Guffa18-May-08 1:40
Guffa18-May-08 1:40 
GeneralRe: my Dictionary removal code correct? Pin
George_George18-May-08 1:43
George_George18-May-08 1:43 
AnswerRe: my Dictionary removal code correct? Pin
Roger Alsing17-May-08 7:22
Roger Alsing17-May-08 7:22 
GeneralRe: my Dictionary removal code correct? Pin
George_George17-May-08 21:07
George_George17-May-08 21:07 
Questionoverriding Pin
angels77717-May-08 3:09
angels77717-May-08 3:09 

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.