Mob
Mob

Reputation: 11098

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?

Upvotes: 38

Views: 91578

Answers (9)

testing_22
testing_22

Reputation: 2585

When working with Visual Studio, it is possible to compile and run a individual C# class files without any major configuration. Here’s how I've done:

  1. Open your desired CS File: Just make sure it's opened.

  2. Configure a new Startup: On the left side from the second green run button, click Startup Project (Highlighted in the picture). configure_startup_project

  3. Choose the option Current Selection: Then click apply.

choose_current_selection

That’s it! You are now able to run your single C# file.

Upvotes: 0

sheetal
sheetal

Reputation: 21

I am new to dot net world and facing same issue. To practice C# need to write several small small code in diff .cs files but again as per dot net only one Main() can be present.

So if you have 10 .cs file then make sure you update their 'main' method name with small case

 static void main(string[] args){}

And the file you want to run should have Main() method with capital M

 static void Main(string[] args){}

Also even if your .cs file are in some other folder, you need to run command from root folder only

dotnet run

Upvotes: 2

Khaaytil
Khaaytil

Reputation: 63

Changing static to private in files i want to exclude works for me, i leave only 1 file with static and visual studio runs only that one

Upvotes: 2

Bruno Medeiros
Bruno Medeiros

Reputation: 2389

As the OP came from a Java background, I think it worth answering this question in a slightly different way.

I understand the the OP has a few different C# classes with their corresponding static Main() methods (each one of these classes probably being a different way to bootstrap the app code) and wants to easily switch between them when launching the app in Visual Studio. So, the short answer is: there is no easy way :(

Firstly, you can not launch a project through a static Main() method if its output type is a "Class Library". You need to change it to either "Console Application" or "Windows Application" to be able to launch it. This is completely different from the Java world where you can simply right-click on any class with a static main() method, then click "Run as...", "Java Application". In Java, there is no such concept of a library project/module, so it doesn't matter if the project that contains this class with static main() is a "library" or not.

Secondly, the easiest way I found is (very similar to Ray's answer):

  1. Right-click on the project, then "Properties"
  2. In "Application", make sure your output type is not "Class Library"
  3. Change "Startup Object" to the class whose static Main() is the one you want to run
  4. Right-click on your project, "Debug", "Start New Instance"

Unfortunately, I see no way to save in VS different launch configurations for the same project using different classes, so that you can easily switch between them. AFAICS the only way to achieve that is to create new projects inside the solution and configure them to launch the different main classes.

Upvotes: 11

rishikandra
rishikandra

Reputation: 31

You can add the static main(string[] args) method to the class you want to run or make and object of the same class in program.cs and call the methods you need in the main() of program.cs

Upvotes: 3

Amit Mittal
Amit Mittal

Reputation: 1127

Only that class will run first which contains the "static Main" method. Add a Main() method to the class you want to execute first.

Revert me if you need more help. Hope this helps.

Upvotes: 0

Ray
Ray

Reputation: 46555

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.

Upvotes: 10

Jude Cooray
Jude Cooray

Reputation: 19862

Program.cs contains what to run when the project is run.

Application.Run(new Form1());

You can use that to select any class to be run.

If it's a console application, only the class with the entry point is run.

static void Main(string[] args)
{
}

Upvotes: 0

Bas
Bas

Reputation: 27085

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.

Upvotes: 10

Related Questions