Dictionary returning a default value if the key does not exist [duplicate]

This question already has answers here: Is there an IDictionary implementation that, on missing key, returns the default value instead of throwing? (18 answers) Closed 9 years ago. I find myself using the current pattern quite often in my code nowadays var dictionary = new Dictionary<type, IList<othertype>>(); // Add stuff to dictionary var somethingElse = … Read more

Does List guarantee insertion order?

Say I have 3 strings in a List (e.g. “1”,”2″,”3″). Then I want to reorder them to place “2” in position 1 (e.g. “2”,”1″,”3″). I am using this code (setting indexToMoveTo to 1): listInstance.Remove(itemToMove); listInstance.Insert(indexToMoveTo, itemToMove); This seems to work, but I am occasionally getting strange results; sometimes the order is incorrect or items from … Read more

How to randomize two ArrayLists in the same fashion?

I have two arraylist filelist and imgList which related to each other, e.g. “H1.txt” related to “e1.jpg”. How to automatically randomized the list of imgList according to the randomization of fileList? Like in excel, if we sort certain column, the other column will automatically follow? String [] file = {“H1.txt”,”H2.txt”,”H3.txt”,”M4.txt”,”M5.txt”,”M6.txt”}; ArrayList<String> fileList = new ArrayList<String>(Arrays.asList(file)); … Read more

How do I overload the square-bracket operator in C#?

DataGridView, for example, lets you do this: DataGridView dgv = …; DataGridViewCell cell = dgv[1,5]; but for the life of me I can’t find the documentation on the index/square-bracket operator. What do they call it? Where is it implemented? Can it throw? How can I do the same thing in my own classes? ETA: Thanks … Read more