Move a type and nested types into separate files, update callers and see unsafe moves refused. Real VS Code footage with English narration and captions.

Move C# types into their own files

Read the guide: Refactoring & Code Actions

Transcript

Visual Studio can move a class to its own file. SharpLsp does that in VS Code, and then goes further: it moves nested classes out.

Install SharpLsp from the VS Code Marketplace.

Subscribe for .NET, C# and F# content.

One: Receipt shares a file with Order. Open the code actions on its name, and move it to Receipt.cs.

The new file keeps the usings and the namespace. Order's file keeps everything else.

Two: LineItem is a private record nested inside Order. Visual Studio stops here. SharpLsp moves it out.

Private becomes internal, because a top-level type can't be private. And the helper it borrowed is now written as Order.Round, so it still compiles.

Three: Status is public, and Program.cs refers to it as Order.Status.

Move it out, and every caller in the solution follows: the Order prefix is gone, and nothing is left to fix by hand.

Four: Totals calls a private method of Order. Moved out, it could never compile.

So the action simply isn't there. SharpLsp never offers a move it can't finish, and if a move would still break the build, it tells you why instead of applying it.

Move a type to its own file. Move a nested type out, with its callers rewritten. And nothing offered that can't work.

Install SharpLsp, and subscribe for .NET, C# and F# content!