How to compile just one file in c#?

A Visual Studio add-in tool like ReSharper is a very good investment for this situation.

ReSharper performs continuous background solution-wide code analysis and will report issues by conveniently displaying a bar next to your code file\document scrollbar which has red\orange lines denoting any lines of code that have issues\errors. The displayed lines are click-able to navigate to the line in question and also have tool-tips detailing what the exact problem is:

http://www.jetbrains.com/resharper/features/code_analysis.html#Continuous_Code_Quality_Analysis

           http://www.jetbrains.com/resharper/features/screenshots/50/marker_bar.png

The issues\warnings that ReSharper can check for are configurable (but it has excellent configuration out-of-the-box), and can denote anything from errors which would cause the code not to compile to more subtle issues where it has detected a possible null method call result which has not been explicitly checked for.


In command line: %windir%\Microsoft.Net\framework\V3.5\csc.exe /target:library File.cs

You could reasonably attach this to the solution explorers context menu through Tools->External Tools

set the arguments to /target:library $(ItemPath)

something like that might do what you want. Though the file would have to depend on no other files in the project or in referenced binaries aside from what's in the GAC.


No it is not possible to do this in C#.

Unlike C++ a C# file cannot be reasonably compiled on it's own to determine if it has any errors. C++ achieves this through #include statements which allows a .cpp file to understand the declaration of types available. These declarations define the structure of types the current file depends on and allows the compiler to ensure they are used according to specification.

This process is handled implicitly in C#. The set of declarations available is simply the set of all declarations in all compiled files which are otherwise accessible. There is no way to forward declare dependencies in the manner C++ does and hence no way to ensure they are being used correctly within a single file.


For single .cs file compile + run:

  1. In VS 2008, go to "Tools" > "External Tools"
  2. Click on "Add"
  3. Title: Run CSC (or whatever you want)
  4. Command: C:\Windows\system32\cmd.exe
  5. Arguments: /c C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /target:winexe $(ItemPath) && $(ItemFileName)
  6. Initial directory: $(ItemDir)
  7. Check Use Output Window
  8. Apply + Ok
  9. Go to Tools and choose "Run CSC"

If this doesn't work, verify that your paths for cmd and csc match.