Reputation: 198368
I'm from Java to C# these days, and using Visual C# 2010 express.
Sometimes I want to write and test some simple code, but I found it not as easy as in Eclipse: write a main
method in any java file, and run it.
In VC#, when I add a Main
method in a .cs file, it will notice me there are two Main
entry in this project. I have to rename the other one before running, and have to rename it back later.
Is there an easy way to do this?
Upvotes: 2
Views: 1966
Reputation: 593
As solution why not adding public testXXXX() method to every class you want to test and then have 1 main method in which you would call those testXXXX() methods.
Upvotes: 0
Reputation: 151720
In VC#, when I add a Main method in a .cs file, it will notice me there are two Main entry in this project.
Then don't add another main
method. The file Program.cs will contain a main method when you've created a Console or WinForms application. If you want to test something, for example a class you've written, simply instantiate that class in the existing main method.
Upvotes: 0
Reputation: 26792
The mono compiler includes a REPL utility (http://www.mono-project.com/CsharpRepl). Something similar is supposed to be included in a future version of C# / Visual Studio (see the latest CTP of the Roslyn project).
Another possibility is the 'test driven .Net' utility, but that doesn't work with Visual Studio express. It (a.o.) allows you to run any method directly from within visual studio.
LINQPad and Snippet Compiler are also viable options.
You can also simply create a .cs file and compile it on the command line. For example, put this code in a file called hello.cs
using System;
public class Program {
public static void Main() {
Console.WriteLine("Hello World");
}
}
Then on the command line, compile with csc hello.cs
=> this creates hello.exe which you can then run.
Upvotes: 1
Reputation: 69280
As @Alexander R says, LINQPad is a great tool. I use it extensively for small snippets, like testing answers I post to Stack Overflow.
If I have to be able to call other code in my current project I add a unit test project to my solution. When I want to test something, I write the code in a new test method and then use the Ctrl+R, Ctrl+T shortcut to run the currently selected test method.
Upvotes: 1
Reputation: 2476
Visual Studio works better for large projects. If you just want to test some code then I'd either stick it in the main method of your application, or forget VS and run it through a code scratchpad.
The best C# code scratchpad out there is LINQPad
Of course, if you're testing a lot you may need to stub out any dependencies that the code has. It's difficult to say without knowing the scope of your code, or the type of project.
Upvotes: 2