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