A tutorial for getting started with C# single file programs and a collection of samples.
You need to have at least .NET 10 installed for single file programs to work.
Run the installer.
You can't just apt-get the latest SDK, at least right now while it's prerelease. Here are the steps that worked for me:
curl -LO 'https://builds.dotnet.microsoft.com/dotnet/Sdk/10.0.100-rc.1.25451.107/dotnet-sdk-10.0.100-rc.1.25451.107-linux-x64.tar.gz'
sudo mkdir -p /usr/local/share/dotnet
sudo tar -C /usr/local/share/dotnet -xzf dotnet-sdk-10.0.100-rc.1.25451.107-linux-x64.tar.gz
sudo chmod +x /usr/local/share/dotnet/dotnet
# test it works
/usr/local/share/dotnet/dotnet --info
# create symlink to `dotnet` command
sudo ln -sf /usr/local/share/dotnet/dotnet /usr/local/bin/dotnet
# test just 'dotnet' works now
dotnet --info
# should return /usr/local/bin/dotnet
which dotnetYou can test it out with a Hello World program by just adding
Console.WriteLine("Hello World");to a file hello.cs and then running dotnet run hello.cs.
You should see Hello World print out.
If you don't want to have to use dotnet run you can remove the file extension (optional), chmod +x hello, and add a "shebang" syntax to the top of the file indicating how it should be run. The whole hello file would then be:
#!/usr/bin/env dotnet
Console.WriteLine("Hello, World!");NOTE: Line endings can mess this up. If you get weird errors or see \r in the output you probably need to run dos2linux *filename* to convert to unix line endings.
So at this point we can:
dotnet run hellodotnet hello./hello
And each one should yield the same result: