views:

87

answers:

4

In VC++ I can press CTRL+F7 to compile a single file, or right click on a source file ot compile it. Is it possible to compile a single file (or current file) in C#?

I would like to for example know if my current file has any errors in it without having to compile everything.

+4  A: 

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.

JaredPar
Couldn't it in theory though parse everything it needs and ignore all errors except the ones of types used in my current source file? So it only reports the ones needed.
Winforms
@Winforms, why not just compile and sort the error list by file name and scroll to the file in question?
JaredPar
I guess, when in the middle of refactoring a large peice of code though I'd like to only see errors for that file, but I see your point. Just one of my annoyances since moving to C#
Winforms
+3  A: 
chibacity
A: 

Shift-F6 will compile the current assembly, which is almost what you want.

Ian P
A: 

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.

Jimmy Hoffa