Tiny top-level programs with C# 9 and SmallSharp and Visual Studio
One of the things I'm always working on and am always excited about is making C# simpler for new folks.
With .NET 5, today, this works, as it includes C# 9
> dotnet new console
The template "Console Application" was created successfully
> echo System.Console.WriteLine("Hello World"); > program.cs
> dotnet run
Hello World
That's C# 9 top level programs. We should be able to remove even more. Skip the first command completely and do it all on one line, one file.
A hello world is either
using System;
Console.WriteLine("Hello World!");
or
System.Console.WriteLine("Hello World!");
or, scandalously
using System;
using static System.Console;
WriteLine("Hello World!");
Not sure how I feel about that last one. Regardless...would this work in Visual Studio 2019? What if I was teaching a class and wanted to have one file per homework assignment, for example? Right now Visual Studio only supports one top-level program per project. Make sense why, but for learning, why not allow folks to choose from the run/debug menu?
I'm going to add a reference to SmallSharp like this (or in Visual Studio)
> dotnet add package smallsharp
Now here's what my homework looks like in Visual Studio 2019! There's one menu item per top level program!
This lovely prototype was done by Daniel Cazzulino (kzu) and you can learn more at https://github.com/devlooped/SmallSharp, or just try it out as I have here!
What do you think? How can small top-level programs help new people?
What about this?
> dotnet new microservice
> dotnet run
Sound off in the comments. How tight and simple would that be?
Sponsor: Tired of not finding the code you're looking for? Get Sourcegraph universal code search and search code across ALL your repos, languages, and code hosts. Find and fix code fast with Sourcegraph. Try it now!
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
I expect really it makes it easier to teach than it does to learn.
If (expression)
Some code
If (variable is null)
Some code
If (variable is not null)
Some code
// Example var usage and async programming
var result = await respository.DoSomething();
to be really something which I think I would
never understand. It seems too complicated and very broad for me.
I'm looking forward for your next post, I'll try to get the hang of it!
I just pushed the latest v1.1 version that fixes some minor issues users reported, and made it so now just changing the active/opened document, automatically selects it for "startup file" and you never need to even click on the dropdown at all. It's quite magical, if I might say so myself :)
See the latest readme.
Cheers!
Comments are closed.
set-alias d dotnet
d new web && d run