• Stars
    star
    279
  • Rank 147,967 (Top 3 %)
  • Language
    C#
  • License
    MIT License
  • Created about 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Create, edit and run multiple C# top-level programs in the same project by just selecting the startup program from the start button.

Icon SmallSharp

Version Downloads License GH CI Status

Icon

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 Christian Findlay C. Augusto Proiete Kirill Osenkov MFB Technologies, Inc. SandRock Eric C Andy Gocke

Sponsor this project  

Learn more about GitHub Sponsors

More Repositories

1

moq

The most popular and friendly mocking framework for .NET
C#
5,709
star
2

GitInfo

Git and SemVer Info from MSBuild, C# and VB
Pascal
513
star
3

ThisAssembly

Exposes project and assembly level information as constants in the ThisAssembly class using source generators powered by Roslyn.
C#
418
star
4

nugetizer

A simple to understand packing model for authoring NuGet packages
C#
253
star
5

avatar

A modern compile-time generated interception/proxy library
C#
139
star
6

NuDoq

A standalone API to read .NET XML documentation files and optionally augment it with reflection information.
C#
105
star
7

dotnet-file

Download, update and sync loose files from URLs
C#
52
star
8

dotnet-vs

A global tool for managing Visual Studio installations
C#
49
star
9

DependencyInjection.Attributed

Provides compile-time discovery and code generation of service registrations from attributed types
C#
36
star
10

CloudActors

An opinionated, simplified and uniform Cloud Native actors' library that integrates with Microsoft Orleans.
C#
33
star
11

RxFree

An ultra-lightweight Rx source-only nuget to avoid depending on the full System.Reactive for IObservable<T> producers
C#
32
star
12

SponsorLink

SponsorLink: an attempt at OSS sustainability
C#
31
star
13

xunit.assemblyfixture

Provides per-assembly fixture state support for xunit
C#
24
star
14

WebSocketPipe

System.IO.Pipelines API adapter for System.Net.WebSockets
C#
24
star
15

Merq

Internal application architecture via command and event messages
C#
23
star
16

TableStorage

Repository pattern with POCO object support for storing to Azure / Cosmos DB Table Storage
C#
21
star
17

Injector

Allows injecting .NET code into another Windows process
C++
12
star
18

WebSocketeer

High-performance intuitive API for Azure Web PubSub protobuf subprotocol
C#
11
star
19

xunit.vsix

Xunit for creating VSIX integration tests
C#
10
star
20

dotnet-tor

A .NET cross-platform CLI app that uses TorSharp to run a local proxy
C#
10
star
21

oss

Basic repo configuration for my OSS projects
SCSS
10
star
22

azdo

Home for azdo.io and accompanying linkinator source
C#
9
star
23

Dynamically

Instantiate record types from dynamic data with compatible structural shapes, in-memory with no reflection or serialization, via compile-time source generators.
C#
9
star
24

Web

XLinq to Web
HTML
8
star
25

chromium

Run a portable Chromium using dotnet 6+ and nuget.
C#
8
star
26

WebSocketChannel

High-performance System.Threading.Channels API adapter for System.Net.WebSockets
C#
8
star
27

dotnet-evergreen

A dotnet tool runner that automatically updates the tool package before and while running it
C#
6
star
28

CredentialManager

Packages the Git Credential Manager cross-platform implementation for Windows, macOS and Linux for use as a generic credential manager.
C#
6
star
29

dotnet-gcm

A dotnet global tool to interface with the Git Credentials Manager Core
C#
5
star
30

dotnet-eventgrid

Azure Function app and accompanying dotnet global tool to monitor Azure EventGrid streams in real-time.
C#
5
star
31

System.Diagnostics.Tracer

An improved API on top of System.Diagnostics
C#
5
star
32

json

JsonPeek and JsonPoke tasks implementations
C#
5
star
33

dotnet-stop

Gracefully stops processes by sending them SIGINT (Ctrl+C) in a cross platform way.
C#
4
star
34

nosayudamos.org

C#
3
star
35

catbag

A repository of loose helpers, base clases and assorted code
C#
3
star
36

scraper

A web scraping app that runs on Azure Container Apps
C#
3
star
37

actions-bot

A GitHub Action that sets bot secrets as git defaults if present
3
star
38

tsh

C#
2
star
39

cloudy

Spike on cloud-first architecture for apps, heavily event-driven
C#
2
star
40

PackageReferenceCleaner

Clean your PackageReferences with PrivateAssets=all into beautiful one-liners, automatically
C#
2
star
41

shields

Custom endpoints for custom badges using https://shields.io/endpoint
C#
2
star
42

Cognitive

Unified Cognitive API for Azure, Amazon and Google
C#
2
star
43

html

Loads Html documents as XLinq XDocuments
HTML
2
star
44

epub

A lightweight library that implements EPUB standard
C#
2
star
45

sponsors

Automatically updated list of devlooped sponsors
1
star
46

yaml

YamlPeek MSBuild Task
C#
1
star
47

actions-includes

Processes include HTML comments in files and inserts the included files
PowerShell
1
star
48

jq

A nuget distribution of the official JQ implementation, for easy consumption from .NET
C#
1
star
49

Azure.Functions.OpenApi

A zero-code OpenAPI (Swagger) basic generator for Azure Functions
C#
1
star
50

Mvp.Xml

C#
1
star
51

StreamR

An opinionated streaming-based library for running assistants with support for tool/function invocation and assistant behavior composition.
C#
1
star