A tip for quickly and safely changing a namespace

If you are using Visual Studio, you can change the namespace of a class by using a Quick Action (cntl + .) on the name of the namespace to change and select, in this case, ‘Move contents to namespace…’

Screenshot 2023-05-08 084945

The beauty of this is VS will take care of updating any files that have dependencies on the things that have moved to the new namespace for you. No having to chase them down yourself as Sam does here. :slight_smile:

To be fair, I don’t think this existed at the time this was recorded so we get a demonstration of the previous pain of changing a namespace.

4 Likes

I think it’s been around for a while in VS. Sam used VSCode, though, and it may not be there, although I don’t think that’s the case. Maybe Sam just didn’t know, or chose not to use it.

As for VS tips; here’s another. When you change the name of a variable or method, you can press F2 (or with my key binding scheme - Ctrl+R, Ctrl+R) and VS will also update all the references to the variable or method in this same way for you.

1 Like

:+1: I use that one all the time (and am also a Ctrl+R, Ctrl+R binding scheme).

Along similar lines, renaming a file in the Solution Explorer that contains a class with the same name allows one to automatically rename the class to the same name and propagate that change throughout the solution, just like the in-file rename.

1 Like

I was going to mention this one, too, but then didn’t…

Privacy & Terms