Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring a Dictionary, changing key type

Tags:

c#

refactoring

I am refactoring a C# library that uses a Dictionary<int, SomeInterface> all over the system. I need to change the key type from int to string, and use a Dictionary<string, SomeInterface> instead, and I am making dozens of changes. How to do it better, how to define a type in one place and uses it everywhere, so that if I need to change it later, I can change it in one place?

I already added a GetKey() method to my interface.

Edit: the library is used in other DLLs, and I want to limit possible changes to only one library. This is an agile shop, so we need to be ready for changes.

like image 609
Arne Lund Avatar asked May 11 '26 02:05

Arne Lund


1 Answers

If swapping the type of key is important to you, consider a custom class wrapping the underlying key.

e.g, public class Key { public Key(object adaptee) { ... } }

Remember to implement Equals() and GetHashCode() and just delegate to the underlying object.

I'm not sure if this helps your particular scenario, but in general, Resharper is a fantastic tool to help assist with these types of changes.

like image 188
Kevin Avatar answered May 12 '26 16:05

Kevin