How to quickly run some C# without a project

01 Oct 2020

Do you ever wish you could just quickly run some C# code, without all the heavy lifting, like creating a new project, setting it up etc? Or worse yet, starting to experiment with some temporary code by modifying an existing solution, and then forgetting to remove your code, and lo and behold, the next thing you know it’s deployed to production?!

Wouldn’t it be nice to be able to play around with any C# code, as if from a command line, but with all the goodies like highlighting and code completion? How awesome it’d be to see all the properties on the objects you’re working with and their actual values, without having to rely on some vague description from MSDN? Think of this as iPython or dev tools Javascript console, but for C#.

That would let you try different ways to solve problems, running code straight away, line by line, without having to restart your project and step into whichever function you’d be modifying instead.

C# Interactive

If you’re on Windows, this is your lucky day (if you’re on macOS or Linux – don’t despair, read on, there’s a solution for you too). Both paid-for Visual Studio and free Visual Studio Community Edition have this awesome feature - C# Interactive.

To open C# Interactive panel, go to “View” -> “Other Windows” -> “C# Interactive”.

C# Interactive screenshot

If you need to load additional assemblies (3rd party SDKs / libraries), you can use #r command and provide a full path to the assembly:

C# interactive load assembly

Also, run #help to see useful keyboard shortcuts that save you time when re-running and editing commands.

.NET Fiddle

I have previously mentioned .NET Fiddle in the article on tools for .NET and Web development. It’s an awesome online REPL for C# (as well as F# and VB.NET). It’s the only online REPL that supports latest (at the time of writing) C# 8.0 features

.NET Fiddle - online C# REPL

Similar to C# Interactive in VS, it has syntax highlighting, code completion and loading of libraries – although the choice of is somewhat constrained to a pre-defined list.

dotnet-script

If you are on macOS or Linux, but still want to use a local C# REPL, you can try dotnet-script. Although it doesn’t have all the standard features such as autocompletion and code highlighting, it does the job just fine.

Also, you can use it as a script execution engine for your C# code – imagine writing your shell scripts in C# instead of bash/zsh/PowerShell!

It supports loading of NuGet packages in much better way than C# Interactive. Refer to the documentation for #r and #load commands:

❯ dotnet script
> #r "nuget: AutoMapper, 6.1.0"
> using AutoMapper;
> typeof(MapperConfiguration)
[AutoMapper.MapperConfiguration]

Conclusion

Hopefully, this article helps you iterate faster and better when experimenting with C#, and cut down the time spent endlessly restarting your debugging sessions just when you need to change one line of code to try out something new.

Liked this article? Join my mailing list to get helpful advice, tips & reviews of dev tools for .NET development.

Subscribe now and get helpful tips on developing .NET apps - never miss a new article.

You can unsubscribe at any time. I'll never share your email with anyone else.