Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile and run a single class file cs file?

Sorry if this is trivial, I am new to Visual Studio, I have a single project in which contains multiple class files (.cs) files, how do I run each one individually. Whenever I go to debug, it selects only a single .cs file. Thanks.

Edit : Coming from a java background using netbeans, it is possible to have a package with several .java files in the package, provided each of the .java files have a main method they can be individually compiled and ran. Is something like this available in Visual Studio?

like image 637
Mob Avatar asked Jan 03 '12 12:01

Mob


People also ask

How do I compile a .cs file?

Open the command prompt tool and go to the directory where you saved the file. Type csc helloworld. cs and press enter to compile your code. If there are no errors in your code, the command prompt takes you to the next line and generates helloworld.exe executable file.

Can I run a single C# file?

You can run a C# project or . csproj file if it's a runnable program. If the project contains a C# file with a Main method, and its output is an executable or .exe file, it will probably run if it builds successfully. If your program code is already in a Visual Studio project, open the project.

How do I compile a class in Visual Studio?

Ctrl+F7 will compile only the active source file. Look for the Compile item at the bottom of the Build menu. Of course, you'll still have to do a build before you can test, but if you just want a quick sanity check after modifying a source file, this can be handy.


2 Answers

If you want to select which Main method gets run, you can select that in Project -> Properties under Startup Object. There are various requirements that need to be met (like being static) and you can only select one at a time.

If you want to call the main method on multiple static classes, you'll need to create a main one that calls the other ones. You could get complicated and use reflection to search your project for the classes, but it's much more work than just statically calling them.

like image 70
Ray Avatar answered Sep 19 '22 11:09

Ray


Normally, you cannot build a single CS file unless you add it to a separate project. Visual Studio automatically builds all CS files in a project.

If you only want to build a single file you can change this in the settings of the file:

Click the files you do not want to build, look at the properties window (F4).

enter image description here

Set build action to None to disable building that file.

like image 40
Bas Avatar answered Sep 23 '22 11:09

Bas