37

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?

8
  • 3
    I'm assuming each has a main function?
    – Ayush
    Commented Jan 3, 2012 at 12:15
  • 2
    What type of project is this? Why run a class individually? What are you trying to achieve?
    – Oded
    Commented Jan 3, 2012 at 12:15
  • 3
    Probably the author wants to do some testing and has multiple files with a Main method
    – Bas
    Commented Jan 3, 2012 at 12:17
  • 4
    It's interesting how people from C# world can't even understand the question (given how worse VS is compared to any Java IDE).. It's so easy to choose which class you want to use to bootstrap your code in any Java IDE, an so hard to do the same in VS. I'm struggling for minutes to run a simple Main() method I created in a library project, it seems I'll need to add another console project to the solution to be able to achieve that. Commented Dec 19, 2017 at 6:35
  • 2
    @BrunoJCM haha, that's completely true. I am going on other direction, as a mainly .net developer, I am finding how easy to achieve things in Java world. Commented Jan 31, 2018 at 21:04

9 Answers 9

11

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.

3
  • 1
    There is no Application tab in project properties.
    – Sid
    Commented Dec 2, 2020 at 10:32
  • No project is visible only a folder i created.
    – Gizzmo
    Commented May 28 at 15:10
  • you need to create projects (modules) inside your solution, not just folders. Commented May 29 at 17:57
10

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.

1
  • 3
    I thought he was talking about 'running' the files not building them.
    – Ray
    Commented Jan 3, 2012 at 12:24
10

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.

0
3

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

2

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

2

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
0

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)
{
}
0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.