Skip to content

ardalis/CSharp-Single-File-Scripts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

CSharp Single File Apps / Programs / Scripts

A tutorial for getting started with C# single file programs and a collection of samples.

Getting Started

You need to have at least .NET 10 installed for single file programs to work.

Download .NET 10 SDK

Windows

Run the installer.

Ubuntu

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 dotnet

Create the C# Single Page Program

You 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.

Ways to Run hello

So at this point we can:

  • dotnet run hello
  • dotnet hello
  • ./hello

And each one should yield the same result:

image

References

About

A tutorial for getting started with C# single file scripts and a collection of samples.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages