Scott Hanselman

Tiny top-level programs with C# 9 and SmallSharp and Visual Studio

February 09, 2021 Comment on this post [7] Posted in DotNetCore | Open Source | VS2019
Sponsored By

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!

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.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
February 11, 2021 16:53
What about less characters?

set-alias d dotnet

d new web && d run
February 11, 2021 18:13
I think it's a false economy using the "to make learning easier". In real life you are never going to use that on a project of any consequence. Thus when you join your first team you'll have to learn stuff that replaces all this.

I expect really it makes it easier to teach than it does to learn.
February 12, 2021 1:59
I don't like it... C# has seem to have gone from the SmallTalk/Java "Everything is an object so everything must be in a class" style, to the Pascal/Basic "Just dump the code anywhere" style, skipping right over (my preferred) C/C++ "Tuck the main code in a free-standing function called main()" style. I feel it gets the best balance of conciseness with tidiness.
February 12, 2021 5:52
Terse, elegant, self evident is always good. This is why I miss Visual Basic.NET Its sad Microsoft largely ignored and abandoned that once great development environment. Its popularity at its peek eclipsed what C# has managed to do. That said C# is introducing simpler idioms with each release which is good to see.

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();

February 13, 2021 7:09
You actually make it seem so easy with your presentation but I find this topic
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!
February 14, 2021 14:44
Although LINQPad is a great tool, not everyone wants to install it. SmallSharp makes it easy to share a repository of sample snippets.
February 16, 2021 4:07
Thanks Scott for raising the awareness to the awesomeness that it is for newcomers to C# the top-level programs feature and my little contribution to the space.

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.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.