4

I mean like in c++ you can do cl.exe main.cpp

I don't have really any reason to do that, just curious

3 Answers 3

5

Assuming by "script" you mean "normal C# source file" then yes, absolutely, if csc is available to you:

Program.cs:

using System;
Console.WriteLine("Hello world");

Compile:

csc Program.cs

Run:

./Program.exe

Now, decompiling that, it looks like it's targeting .NET Framework rather than .NET Core by default, but I'd expect it to be feasible to change that if you really want to, by specifying a custom response file. It does make the latest C# features available to you, but if you use C# features that are only supported on later versions of .NET (e.g. default interface implementations) then I suspect you'd have to explicitly specify the appropriate standard libraries rather than using the default .NET Framework ones.

If csc isn't available to you as an executable, the DLL may still be available in the .NET SDK directory, under Roslyn/bincore.

For example, on my WSL2 Ubuntu installation, just trying to run csc fails, but the command below definitely invokes csc:

dotnet /usr/share/dotnet/sdk/6.0.202/Roslyn/bincore/csc.dll

The bad news is that trying to use that to just compile a standalone program requires providing library references etc - that's probably more hassle than it's worth.

Sign up to request clarification or add additional context in comments.

6 Comments

this seems just for .net framework and not core
@MAR1: The version of csc I'm running isn't part of the .NET Framework (it's installed as part of MSBuild in VS2022), but I agree it's not in the .NET Core SDK. I don't have time to find details right now, but I'll update the answer briefly now and try to find how to install it on a "just .NET Core" system later. (Do you need it on Linux?)
i don't really need it i am just curious about how to do it, I am using windows
@MAR1: Okay, so if you're on Windows and you've got VS2022 installed, it should just work. What exactly do you mean by "and not core"? In terms of standard library defaults?
it doesnt support lateset c# versions
|
1

No - but, for what you may be looking for, the dotnet new console command generates a default csproj that runs the Program.cs file that is also generated. With the new(ish) top level statement files, you can just insert a few usings and your script code, and just use dotnet run

If you're using an IDE, it'll run all the dotnet commands for you though

1 Comment

Links can be broken over time. So it quoting is better than just sharing links.
0

Yes C# has a Scripting concept(.csx). It also has a REPL (csi.exe)

References : https://learn.microsoft.com/en-us/archive/msdn-magazine/2016/january/essential-net-csharp-scripting

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.