Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#
Tip/Trick

Using VS Code for C# Scripts

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
17 Jun 2020CPOL2 min read 12.9K   130   11   6
Using VS Code for C# scripts with execute capability
Using VS Code for C# scripts and snippet testing with execute capability, and not losing things in a multitude of ConsoleApp folders.

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:

XML
<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().

C#
// script.cs
using System;

namespace script
{
    public class program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello world!");
        }
    }
}
C#
// 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:

C#
// 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:

XML
<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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect -
United Kingdom United Kingdom
Mehdi first started programming when he was 8 on BBC+128k machine in 6512 processor language, after various hardware and software changes he eventually came across .net and c# which he has been using since v1.0.
He is formally educated as a system analyst Industrial engineer, but his programming passion continues.

* Mehdi is the 5th person to get 6 out of 7 Platinum's on Code-Project (13th Jan'12)
* Mehdi is the 3rd person to get 7 out of 7 Platinum's on Code-Project (26th Aug'16)

Comments and Discussions

 
PraiseIt could be done by VS 2017 Pin
wmjordan22-Jun-20 15:42
professionalwmjordan22-Jun-20 15:42 
I'd been using VS Community version from VS 2013 to VS 2017, VS could do this kind of job much better. Someone in the VS team got their heads kicked by donkeys and decided to remove the create temporary project capability from VS 2019. Now we have to find some other tools to do that.

Thanks for the tip!

GeneralRe: It could be done by VS 2017 Pin
Mehdi Gholam22-Jun-20 18:10
Mehdi Gholam22-Jun-20 18:10 
QuestionAwesome Pin
wgra02122-Jun-20 9:56
wgra02122-Jun-20 9:56 
SuggestionOr...LinqPad Pin
mldisibio19-Jun-20 10:27
mldisibio19-Jun-20 10:27 
GeneralRe: Or...LinqPad Pin
Mehdi Gholam21-Jun-20 20:18
Mehdi Gholam21-Jun-20 20:18 
GeneralRe: Or...LinqPad Pin
Kevin McFarlane22-Jun-20 2:08
Kevin McFarlane22-Jun-20 2:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.