4 ms·
Can I just create a single .cs file somewhere, and execute it by typing its name? No Visual Studio solution/project shenanigans? That’s my dream. Top-level pr
by kwertyoowiyop 6y ago
Can I just create a single .cs file somewhere, and execute it by typing its name? No Visual Studio solution/project shenanigans? That’s my dream. Top-level programs to eliminate boilerplate is a nice related step.
- unnouinceput 6y agowhat do you mean by "execute it"? It's source, you need to compile it and get an executable out of it. This is not Python, an interpreter. But if you want an interpreter for C#, nobody's stopping you to write one. Me, on the other hand, I want Python to become a compiled language. And to those who's going to reply with "PyPy/Cython/Nuitka" I have a question for them: -do show me a .dll from those!
- ninkendo 6y agoTo give an example, in Swift, I can write a hello.swift with: #!/usr/bin/env swift print("Hello") And after a chmod +x, it works. Granted, anyone who I distribute this to will need a swift compiler installed, but that’s to be expected (and similar for Python.) Golang and other “compiled” languages have similar ways of making this work too.
- unnouinceput 6y agoOh, I get it. You mean install some sort of interpreter that is called by the OS for that language. Sorry, I live in real world, where customers don't allow you to install a gazzilion files just to run your application, they expect a proper installer that is under strict supervision by their admin team and you only deliver executables.
- 0xcoffee 6y agoI would love to replace all my powershell build scripts with .cs files. imo there is lots of room for C# Scripts.
- ninkendo 6y agoI don’t think the use case for this is anything you’re shipping to “customers”... more for just personal code you can use as scripts/playgrounds. To use the swift example, often I want to double check whether something in the language works as I expect, so I write a quick test.swift and run it. It’s a useful thing even if you don’t personally want it.
- twodave 6y agoVS comes with an interactive C# window that basically serves this purpose. Aside from that, you can always write a unit test and set a breakpoint to inspect whether something works as you think it does. In my opinion both these options are superior to running a file for that use case (though running a C# file would certainly have other uses that tests/REPL aren't sufficient for).
- ISO-morphism 6y agoNot quite at a single .cs file, but there is the third-party dotnet-script [1] installed as a global tool to run .csx files. That's been around longer that C# language support for top level statements. [1] https://github.com/filipw/dotnet-script https://github.com/filipw/dotnet-script
- 0xcoffee 6y agoThere are third party projects that support this: https://github.com/oleg-shilo/cs-script https://github.com/oleg-shilo/cs-script
- flukus 6y agoIt's fairly trivial to write your own for any language, I have a c version where you simply start the file with "#!/usr/bin/env crun" the guts of the script is: gcc -x c -ggdb -Wall -Wextra -Wno-main -Wno-unused-parameter -o $out $CFLAGS -include $header - $LDFLAGS <<-CRUN_EOF #line 2 "$1" $(tail +2 $1) CRUN_EOF Obviously gcc would be replaced with csc, you wouldn't need CFLAGS and you might need a temp file or something.