Using C# like a scripting language
Join the DZone community and get the full member experience.Join For Free
Clift Norris wrote a clever little batch file csrun.bat several years ago. I thought I’d posted it here, but apparently not. If you have a C# program in foo.cs, you can type csrun foo.cs to compile and run the program.
The batch file doesn’t do much at all, but it might change how you think about a C# program. The C# code is still compiled, but since the compilation step is hidden, it feels more like an interpreted language.
When someone says they like interpreted languages, maybe what they really mean is that they enjoy running code quickly without the overhead of starting up an IDE, compiling the code, navigating to the directory containing the compiled executable, etc. This has nothing to do with whether a language is compiled or interpreted.
@echo off REM : Compile and run a C# source file. REM : The C# compiler (csc.exe) must be in your PATH. REM : The command line argument is expected to be something like foo.cs if "%1"=="" goto USAGE csc.exe /nologo /out:%1.exe %1 if ERRORLEVEL 1 goto EXIT %1.exe %2 %3 %4 %5 goto EXIT :USAGE echo You must specify an argument representing the C# file you want to run. :EXIT
This batch file does not set references; you’d need to modify it if you want to reference an assembly.
Update: CsharpRepl is a REPL (read-evaluate-print-loop) that lets you write C# at the command line as you would most scripting languages. CsharpRepl is part of Mono and works cross-platform. Thanks to MikeG in the comments.
Opinions expressed by DZone contributors are their own.