SmallSharp 1.2.0

dotnet add package SmallSharp --version 1.2.0                
NuGet\Install-Package SmallSharp -Version 1.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SmallSharp" Version="1.2.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SmallSharp --version 1.2.0                
#r "nuget: SmallSharp, 1.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SmallSharp as a Cake Addin
#addin nuget:?package=SmallSharp&version=1.2.0

// Install SmallSharp as a Cake Tool
#tool nuget:?package=SmallSharp&version=1.2.0                

Create, edit and run multiple C# top-level programs in the same project 😍

Why

The new-ish C# top-level programs allow a very intuitive, simple and streamlined experience for quickly spiking or learning C#.

One missing thing since their introduction in Visual Studio is that you can only have one such top-level program in a project. This means that in order to prototype or learn a different area of .NET, you'd be forced to either replace your previous top-level program or change it to be a non-compile item somehow so you can keep it around (i.e. rename to a .txt or change its build action).

SmallSharp allows you to select which file should be the top-level program to run, right from the Start button/dropdown (for compilation and launch/debug). Moreover, it will monitor the active file you're editing, and automatically make it the startup file for you!

start button

This list is automatically kept in sync as you add more .cs files to the project. When you select one target C# file, that becomes the only top-level program to be compiled, so you don't have to modify any of the others since they automatically become None items.

All compile files directly under the project directory root are considered top-level programs for selection and compilation purposes. If you need to share code among them, you can place them in subdirectories and those will behave like normal compile items.

Usage

There is no need to install any Visual Studio extension. SmallSharp works by just installing the SmallSharp nuget package in a C# console project.

  1. Create a new Console project:

New Project Dialog

By default, this new console project may not be set up to target net5.0 or use the latest C# version. So click on the project node, and the project file will open in the editor. Make sure you either:

  • Target the recommended framework version (i.e. net6.0):
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

</Project>
  • Or use latest C# language version if targeting another framework:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net472</TargetFramework>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>

</Project>
  1. Install the SmallSharp nuget package using your preferred method:
  • From the Dependencies node, Manage NuGet Packages dialog:

New Project Dialog

  • By just adding it directly to the .csproj:
  <ItemGroup>
    <PackageReference Include="SmallSharp" Version="*" />
  </ItemGroup>
  • Via the dotnet CLI:
> dotnet add package SmallSharp
  • Via the Package Manager console:
PM> install-package SmallSharp
  1. Now open that Program.cs and make changes to the new concise top-level program such as:
using System;
using static System.Console;

WriteLine("Hello World!");

Keep adding as many top-level programs as you need, and switch between them easily by simply changing the active document.

Demo

How It Works

This nuget package leverages in concert the following standalone and otherwise unrelated features of the compiler, nuget, Visual Studio and MSBuild:

  1. The C# compiler only allows one top-level program per compilation.
  2. Launch profiles (the entries in the Run dropdown) are populated from the Properties\launchSettings.json file
  3. Whenever changed, the dropdown selection is persisted as the $(ActiveDebugProfile) MSBuild property in a file named after the project with the .user extension
  4. This file is imported before NuGet-provided MSBuild targets
  5. The $(DefaultItemExcludesInProjectFolder) MSBuild property allows excluding items at the project-level from the automatically added items by the SDK.

Using the above features in concert, SmallSharp essentially does the following:

  • Monitor the active document in VS and emit it as a launchSettings.json profile and set it as the $(ActiveDebugProfile).

  • Exclude .cs files at the project level from being included as <Compile> by the default SDK includes and include them explicitly as <None> instead so they show up in the solution explorer. This prevents the compiler from causing an error for multiple top-level programs.

  • Explicitly include as <Compile> only the $(ActiveDebugProfile) property value.

This basically mean that this it will also work consistently if you use dotnet run from the command-line, since the "Main" file selection is performed exclusively via MSBuild item manipulation.

Finally, there is some lovely COM-based magic to access the active Visual Studio IDE (via DTE) to monitor the currently opened source file to keep it in sync with the launch profile. This is done purely using public COM primitives and equally public VSSDK nuget packages their APIs. This enables some useful integration with the IDE without requiring installing a VS extension from the marketplace and deal with gracefully degrading functionality.

NOTE: If active document tracking fails to initialize properly restarting Visual Studio will almost always fix it. Once tracking starts, it will work consistently from that point on. The Start dropdown is always available as a fallback in case of issues.

Sponsors

Clarius Org Kirill Osenkov MFB Technologies, Inc. Stephen Shaw Torutek DRIVE.NET, Inc. Ashley Medway Keith Pickford Thomas Bolon Kori Francis Toni Wenzel Giorgi Dalakishvili Uno Platform Dan Siegel Reuben Swartz Jacob Foshee alternate text is missing from this package README image Eric Johnson Ix Technologies B.V. David JENNI Jonathan Oleg Kyrylchuk Charley Wu Jakob Tikjøb Andersen Seann Alexander Tino Hager Mark Seemann Ken Bonny Simon Cropp agileworks-eu sorahex Zheyu Shen Vezel ChilliCream 4OTC

Sponsor this project  

Learn more about GitHub Sponsors

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 160 7/17/2024
1.1.7 1,187 11/16/2022
1.1.6 318 11/16/2022
1.1.5 525 2/3/2022
1.1.4 440 7/5/2021
1.1.2 362 5/17/2021
1.1.1 363 4/8/2021
1.1.0 361 2/15/2021
1.0.3 337 2/12/2021
1.0.2 330 2/12/2021
1.0.1 422 11/20/2020
1.0.0 445 11/18/2020
0.3.0 417 10/19/2020
0.2.0 415 10/1/2020