Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically Create a .cs file from a class in Visual Studio 2010

I often code in such way that I put several classes in one cs file to get my code going and then at some point, I want to move every class to a seperate .cs file within my C# project but I have to do this manually by creating a file for every class and cut and paste the class to the new file and then resolve the required namespace one by one for each file. No Fun!

Is there a way to just click right on the class and make visual studio to automatically move the class to a new file under the project tree? I mean, would you please provide such Macro/Snippet(or whatever it is called :) ) and instructions on how to add it to Visual Studio 2010?

like image 387
iCode Avatar asked Jul 26 '11 23:07

iCode


People also ask

How do I create a .CS file in Visual Studio?

Open Visual Studio, and choose Create a new project in the Start window. In the Create a new project window, select All languages, and then choose C# from the dropdown list. Choose Windows from the All platforms list, and choose Console from the All project types list.

How do I create a class file in Visual Studio?

To add a class in a Visual Studio C++ project, in Solution Explorer, right-click the project, choose Add, and then choose Class. This command opens the Add Class dialog box. When you add a class, you must specify a name that is different from classes that already exist in MFC or ATL.

What is .CS file in Visual Studio?

Files with . cs extension are source code files for C# programming language. Introduced by Microsoft for use with the . NET Framework, the file format provides the low-level programming language for writing code that is compiled to generate the final output file in the form of EXE or a DLL.

How do I run a .CS file in Visual Studio code?

Open Program. cs by clicking on it. The first time you open a C# file in Visual Studio Code, OmniSharp loads in the editor. Select Yes when Visual Studio Code prompts you to add the missing assets to build and debug your app.


3 Answers

If anyone is looking for a lightweight solution to this problem I found a free Move Type To File extension specifically for this purpose.

http://visualstudiogallery.msdn.microsoft.com/5746c6ea-1f92-43f0-8bab-ec6f8573cd6a

like image 91
craftworkgames Avatar answered Oct 13 '22 21:10

craftworkgames


I think Resharper has the functionality you want (plus so much more).

like image 38
colithium Avatar answered Oct 13 '22 22:10

colithium


Visual Studio 2017 now has Code Refactoring that includes Moving a class into its own file.

  • For example you can either right click on the class "Foo" and select "Quick Actions and Refactorings" (or the shortcut Ctrl +)
  • Then select "Move type into foo.cs"
like image 39
tb-mtg Avatar answered Oct 13 '22 21:10

tb-mtg