65.9K
CodeProject is changing. Read more.
Home

Using VS Code for C# Scripts

starIconstarIconstarIconstarIconstarIcon

5.00/5 (7 votes)

Jun 18, 2020

CPOL

2 min read

viewsIcon

14098

downloadIcon

133

Using VS Code for C# scripts with execute capability

Introduction

Recently, someone asked on the forums in Code Project, how do you do offline C# code snippet testing? And most people create lots of console solutions to try out code, which is messy and you lose what you have done in the multitude of ConsoleApp folders.

What I have used for many years was my modified NScript C# runner, but recently I have setup VSCode to aid in the writing of C# code with IntelliSense, which is missing when doing it in Notepad.

This allows you to quickly try out things or write C# scripts and not have to bring up the Visual Studio create projects and generally forget what you wanted to do before starting.

What is Needed

All you need is VSCode, the C# language extension for VSCode and the NScript executable which is provided in the download zip file or as source here:

How To Do It

To get VSCode to use IntelliSense on your C# code, you need to have a .csproj file in the folder of your code. The .csproj does not need to have anything in it and is as simple as the following:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
</Project>

One thing to note is to make sure the TargetFramework matches what is installed on your machine. If you get code squiggles, then check the netcoreapp3.1 value matches what you have.

Now You Can

Now you can have all your scripts and snippets in one folder and open them all in VSCode, and just code with full IntelliSense. All you need to do in each file is make sure the namespace differs so VSCode does not complain you have already defined program and Main().

// script.cs
using System;

namespace script
{
    public class program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello world!");
        }
    }
}
// script2.cs
using System;

namespace script2
{
    public class program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello world too!");
        }
    }
}

To run the code, you can do it via the terminal window or command line as follows :

nscript.exe script.cs

NScript Features

There are 2 executables NScript.exe is a console mode script runner which outputs to the console, and NScriptw.exe which is a Windows mode script runner for when you have WinForm scripts and you don't want a black command window to be seen.

If you need to use libraries in your script, you can add comment line to the top of the code to reference any DLL files:

// ref : mylib.dll
// ref : c:\folder\mylib2.dll
using System;
...

If you are happy with your script as is, you can compile it to an executable with the /c flag:

nscript.exe /c script.cs

Debugging in VSCode

To enable debugging and stop dotnet complaining about multiple entry points defined, just add the StartupObject line to the vscode.csproj file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <StartupObject>script.program</StartupObject>
  </PropertyGroup>
</Project>

Obviously VSCode uses dotnet core to compile the code, and NScript uses .NET v4 , so there will be differences but for the purposes of scripts and debugging, you should not have any problems.

History

  • 18th June, 2020: Initial version