Preface
Students who use .NET may have a more painful experience, that is, if you need to write a piece of test code or sample code, you have to create a project to deal with it. Even the most basic console program, using dotnet cli, is also Need to face the following paragraph of the most basic entry code.
using System;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
It's not like python, js, etc. It's as convenient as opening the editor/command line to write the code.
As a mainstream development language, there are naturally specific solutions.
A development tool that veteran .NET programmers may be familiar with is LINQPad , which is a good .NET code drill tool. It also provides F# and SQL language support. The settings can be directly linked to the database for query, which is no less powerful.
Now LINQPad 6 also supports .NET 5, which is also a good development aid.
Although LINQPad also provides a free version, if you want to use advanced features such as NuGet package integration, code prompts, debug, etc., you still need to purchase authorized . If you are financially rich, try to buy the PREMIUM version directly, and the experience is the most The complete LINQPad.
In addition to LINQPad, there are many other options, one of which can get the most native experience of C# is to use C# script directly.
As early as VS 2015 update 1, VS integrated a C# REPL Command-Line Interface (csi.exe), which can directly execute C# statements on the command line. The overall experience is similar to Python REPL and Node.js REPL. At the same time, a new script file format csx was introduced, so that you can use C# to write script files.
Since the .NET Core era, there have been more choices of open source tools, among which dotnet script is a very good C# scripting tool.
Combining VS Code and OmniSharp, using dotnet script can give a pretty good C# script development experience.
Of course, it also supports NuGet packages and debugging, and is open source and free. Thanks to the cross-platform features of related functions, it basically supports all platforms.
Install
The installation of dotnet script itself is relatively easy.
- Install dotnet core 2.1+ sdk, if you use C# 8 language features, you need to install dotnet core 3.1+ sdk
dotnet tool install -g dotnet-script
run 06104aeb12d297
After the installation of dotnet-script is complete, directly run dotnet script
to enter the RPEL mode, and directly run the C# code directly.
~$ dotnet script
> var x = 1;
> x+x
2
For a better experience, you need to install VS Code and OmniShap.
- Install VS Code
- Install the C# plug-in, driven by OmniSharp, so the plug-in will automatically start related services after the installation is complete
use
After installing the above tools, create a new .csx file (such as main.csx), or use the dotnet script init
command to directly initialize a file.
#!/usr/bin/env dotnet-script
Console.WriteLine("Hello world!");
This is a bit like a scripting language.
dotnet script main.csx
directly on the command line.
After all, it is not an interpreted language, C# script still needs to be compiled, so wait for a while and you can see the output.
- How to use command line parameters
Executing the form dotnet script main.csx -- arg1 arg2 arg3
, you can pass arg1,arg2,arg3
as a parameter to the script.
The script code can be obtained from the global collection Args
.
- How to add NuGet packages
#r
command to the head of the script file to install the NuGet package
#r "nuget: AutoMapper, 6.1.0"
Nowadays, nuget.org has also added the reference instruction of'Script & Interactive', which can be easily copied and used.
It should be noted that in VS Code, every time you add a new NuGet package, you need to restart the OmniSharp Server to make the code analysis take effect.
Fortunately, VS Code can directly execute instructions (Ctrl/CMD + Shift + P)
- How to cite other documents
When the amount of code is relatively large, the files can be split to facilitate management, and the #load
command can be used to import between files.
E.g
#load "Demo.csx"
More
For more usage of dotnet script, please refer to Github
Since dotnet script is only a encapsulation of the roslyn API, the execution efficiency is still different compared to directly building and compiling the project.
Therefore, it is not recommended to use it in a production environment.
In the recently launched .NET 6 preview 4, the minimal API was introduced. It only takes a few lines to run a web server.
var app = WebApplication.Create(args);
app.MapGet("/", (Func<string>)(() => "Hello World!"));
app.Run();
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。