Neulich wurde .NET 5 veröffentlicht. [^ dotnet5] .NET 5 unterstützt C # 9. Ich möchte es sofort ausprobieren, möchte es aber nicht auf meinem PC installieren, sondern ein wenig berühren. Daher möchte ich Docker verwenden, um eine Einweg-.NET 5-Entwicklungsumgebung vorzubereiten.
Probieren Sie Play with Docker [^ pwd] aus, eine Einweg-Docker-Ausführungsumgebung in der Cloud, die mit Browsern verwendet werden kann, für die keine Docker-Installation erforderlich ist.
Die Linux-Umgebung von Play with Docker ist bei Ausführung "Alpine Linux 3.12" und die Version von Docker "19.03.11".
$ more /etc/issue
Welcome to Alpine Linux 3.12
Kernel \r on an \m (\l)
$ docker --version
Docker version 19.03.11, build 42e35e61f3
Führen Sie das .NET 5 SDK Docker-Image aus.
Verbinden Sie mit der Option -it
die Standardeingabe / -ausgabe mit der Shell und verbinden Sie den .NET 5 SDK-Container unverändert.
--rm
wird der Container automatisch gelöscht, wenn der Container endet [^ --rm].
Wenn Sie es auf Ihrem PC versuchen, bleiben keine unnötigen Container übrig.$ docker run -it --rm mcr.microsoft.com/dotnet/sdk:5.0
Unable to find image 'mcr.microsoft.com/dotnet/sdk:5.0' locally
5.0: Pulling from dotnet/sdk
bb79b6b2107f: Pull complete
97805e17b1a2: Pull complete
48d36279ea43: Pull complete
5d23a35fbf12: Pull complete
982bc1066a1e: Pull complete
6cc6e848c1f3: Pull complete
df97eda6f03d: Pull complete
7520ee234b82: Pull complete
Digest: sha256:ac49854ff6dcc1a2916ffc0981503f571698458187f925da0c2f2b6a0bec8dee
Status: Downloaded newer image for mcr.microsoft.com/dotnet/sdk:5.0
root@e68a47087ad8:/#
Sie können sehen, dass .NET 5 installiert ist.
root@e68a47087ad8:/# dotnet --version
5.0.100
Die Linux-Distribution scheint Debian zu sein.
root@e68a47087ad8:/# more /etc/issue
Debian GNU/Linux 10 \n \l
Ich möchte eine Konsolen-App erstellen, um die neuen Funktionen von C # 9 auszuprobieren.
root@e68a47087ad8:/# dotnet new console -o MyConsoleApp && cd MyConsoleApp
Getting ready...
The template "Console Application" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on MyConsoleApp/MyConsoleApp.csproj...
Determining projects to restore...
Restored /MyConsoleApp/MyConsoleApp.csproj (in 89 ms).
Restore succeeded.
Das Projekt wurde erstellt.
root@e68a47087ad8:/MyConsoleApp# ls -R
.:
MyConsoleApp.csproj Program.cs obj
./obj:
MyConsoleApp.csproj.nuget.dgspec.json MyConsoleApp.csproj.nuget.g.targets project.nuget.cache
MyConsoleApp.csproj.nuget.g.props project.assets.json
Ich möchte die Datei bearbeiten, aber vim ist nicht enthalten, daher möchte ich sie installieren. Aktualisieren Sie zunächst das Paket.
root@e68a47087ad8:/MyConsoleApp# apt update && apt upgrade
Get:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
Get:2 http://deb.debian.org/debian buster InRelease [121 kB]
Get:3 http://deb.debian.org/debian buster-updates InRelease [51.9 kB]
Get:4 http://security.debian.org/debian-security buster/updates/main amd64 Packages [248 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 Packages [7906 kB]
Get:6 http://deb.debian.org/debian buster-updates/main amd64 Packages [7856 B]
Fetched 8401 kB in 1s (5711 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
1 package can be upgraded. Run 'apt list --upgradable' to see it.
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
tzdata
1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 264 kB of archives.
After this operation, 3072 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://deb.debian.org/debian buster-updates/main amd64 tzdata all 2020d-0+deb10u1 [264 kB]
Fetched 264 kB in 0s (24.0 MB/s)
debconf: delaying package configuration, since apt-utils is not installed
(Reading database ... 9877 files and directories currently installed.)
Preparing to unpack .../tzdata_2020d-0+deb10u1_all.deb ...
Unpacking tzdata (2020d-0+deb10u1) over (2020a-0+deb10u1) ...
Setting up tzdata (2020d-0+deb10u1) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
Current default time zone: 'Etc/UTC'
Local time is now: Sat Nov 14 16:16:39 UTC 2020.
Universal Time is now: Sat Nov 14 16:16:39 UTC 2020.
Run 'dpkg-reconfigure tzdata' if you wish to change it.
Installieren Sie vim.
root@e68a47087ad8:/MyConsoleApp# apt install vim
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libgpm2 vim-common vim-runtime xxd
Suggested packages:
gpm ctags vim-doc vim-scripts
The following NEW packages will be installed:
libgpm2 vim vim-common vim-runtime xxd
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 7425 kB of archives.
After this operation, 33.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://deb.debian.org/debian buster/main amd64 xxd amd64 2:8.1.0875-5 [140 kB]
Get:2 http://deb.debian.org/debian buster/main amd64 vim-common all 2:8.1.0875-5 [195 kB]
Get:3 http://deb.debian.org/debian buster/main amd64 libgpm2 amd64 1.20.7-5 [35.1 kB]
Get:4 http://deb.debian.org/debian buster/main amd64 vim-runtime all 2:8.1.0875-5 [5775 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 vim amd64 2:8.1.0875-5 [1280 kB]
Fetched 7425 kB in 0s (59.9 MB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package xxd.
(Reading database ... 9877 files and directories currently installed.)
Preparing to unpack .../xxd_2%3a8.1.0875-5_amd64.deb ...
Unpacking xxd (2:8.1.0875-5) ...
Selecting previously unselected package vim-common.
Preparing to unpack .../vim-common_2%3a8.1.0875-5_all.deb ...
Unpacking vim-common (2:8.1.0875-5) ...
Selecting previously unselected package libgpm2:amd64.
Preparing to unpack .../libgpm2_1.20.7-5_amd64.deb ...
Unpacking libgpm2:amd64 (1.20.7-5) ...
Selecting previously unselected package vim-runtime.
Preparing to unpack .../vim-runtime_2%3a8.1.0875-5_all.deb ...
Adding 'diversion of /usr/share/vim/vim81/doc/help.txt to /usr/share/vim/vim81/doc/help.txt.vim-tiny by vim-runtime'
Adding 'diversion of /usr/share/vim/vim81/doc/tags to /usr/share/vim/vim81/doc/tags.vim-tiny by vim-runtime'
Unpacking vim-runtime (2:8.1.0875-5) ...
Selecting previously unselected package vim.
Preparing to unpack .../vim_2%3a8.1.0875-5_amd64.deb ...
Unpacking vim (2:8.1.0875-5) ...
Setting up libgpm2:amd64 (1.20.7-5) ...
Setting up xxd (2:8.1.0875-5) ...
Setting up vim-common (2:8.1.0875-5) ...
Setting up vim-runtime (2:8.1.0875-5) ...
Setting up vim (2:8.1.0875-5) ...
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/da/man1/vi.1.gz because associated file /usr/share/man/da/man1/vim.1.gz (of link group vi) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/de/man1/vi.1.gz because associated file /usr/share/man/de/man1/vim.1.gz (of link group vi) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/fr/man1/vi.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group vi) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/it/man1/vi.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group vi) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ja/man1/vi.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group vi) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/pl/man1/vi.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group vi) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ru/man1/vi.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group vi) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/vi.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group vi) doesn't exist
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/da/man1/view.1.gz because associated file /usr/share/man/da/man1/vim.1.gz (of link group view) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/de/man1/view.1.gz because associated file /usr/share/man/de/man1/vim.1.gz (of link group view) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/fr/man1/view.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group view) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/it/man1/view.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group view) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ja/man1/view.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group view) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/pl/man1/view.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group view) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ru/man1/view.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group view) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/view.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group view) doesn't exist
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/da/man1/ex.1.gz because associated file /usr/share/man/da/man1/vim.1.gz (of link group ex) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/de/man1/ex.1.gz because associated file /usr/share/man/de/man1/vim.1.gz (of link group ex) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/fr/man1/ex.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group ex) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/it/man1/ex.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group ex) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ja/man1/ex.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group ex) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/pl/man1/ex.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group ex) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ru/man1/ex.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group ex) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/ex.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group ex) doesn't exist
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/da/man1/editor.1.gz because associated file /usr/share/man/da/man1/vim.1.gz (of link group editor) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/de/man1/editor.1.gz because associated file /usr/share/man/de/man1/vim.1.gz (of link group editor) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/fr/man1/editor.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group editor) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/it/man1/editor.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group editor) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ja/man1/editor.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group editor) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/pl/man1/editor.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group editor) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ru/man1/editor.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group editor) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/editor.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group editor) doesn't exist
Processing triggers for libc-bin (2.28-10) ...
Probieren Sie jetzt den C # 9-Code aus. Einzelheiten finden Sie in dem folgenden Artikel, auf den ich verwiesen habe.
Am Einstiegspunkt können Sie den Code direkt ohne Klasse schreiben. Es scheint für einfache Chargen praktisch zu sein.
root@e68a47087ad8:/MyConsoleApp# vim Program.cs
System.Console.WriteLine("Hello from top.");
Bauen. (In den folgenden Beispielen weggelassen.)
root@e68a47087ad8:/MyConsoleApp# dotnet build
Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
MyConsoleApp -> /MyConsoleApp/bin/Debug/net5.0/MyConsoleApp.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:02.44
root@e68a47087ad8:/MyConsoleApp# dotnet run
Hello from top.
Sie können leicht unveränderliche Objekte erstellen. Wenn die Eigenschaftswerte übereinstimmen, ist "==" auch für Datensatztypen mit unterschiedlichen Referenzen "true". Sie können auch den Ausdruck "with" verwenden, um einen neuen Datensatz mit nur einigen neu geschriebenen Eigenschaften zu erstellen, die ich in diesem Artikel nicht ausprobiert habe.
Die Wertschöpfung scheint schnell zu sein.
root@e68a47087ad8:/MyConsoleApp# vim Program.cs
using static System.Console;
record Money(decimal amount);
class Program {
static void Main() {
var money1 = new Money(10);
WriteLine($"money1: {Money}");
var money2 = new Money(10);
WriteLine($"money2: {money2}");
WriteLine($"money1 == money2: {money1 == money2}");
}
}
root@e68a47087ad8:/MyConsoleApp# dotnet run
money1: Money { amount = 10 }
money2: Money { amount = 10 }
money1 == money2: True
Target typed new[1]
Sie können den neuen Typnamen weglassen, wenn dies durch Typinferenz offensichtlich ist. Bisher wurde von der rechten Seite ein "var" abgeleitet, das jedoch nicht in einer Mitgliedsvariablen verwendet werden konnte. Es scheint auch, dass der Typname weggelassen werden kann, selbst wenn der Typ im Argument oder im Rückgabewert klar ist.
using System.Collections.Generic;
using System.Linq;
class Program {
static readonly Dictionary<int, List<string>> _cache = new();
static void Main() {
_cache.Add(1, new() { "aaa" });
System.Console.WriteLine(_cache[1].First());
}
}
root@e68a47087ad8:/MyConsoleApp# dotnet run
aaa
"Oder", "und", "nicht" wurden dem bedingten Ausdruck hinzugefügt. Als Feature wird es einmal ausgewertet, sodass es einfach erscheint, den Rückgabewert von Ausdrücken mit Nebenwirkungen zu vergleichen. (= Es ist nicht mehr erforderlich, die Ergebnisse in temporären Variablen zu speichern und dann zu vergleichen, damit Nebenwirkungen nicht mehrmals auftreten.) Es scheint auch, dass es zusammen mit dem Mustervergleich präzise geschrieben werden kann.
root@e68a47087ad8:/MyConsoleApp# vim Program.cs
using static System.Console;
var x = 7;
WriteLine($"x is >= 5 and < 10: {x is >= 5 and < 10}");
WriteLine($"x is (<= 0 or 7 or <= 10) and not 15: {x is (<= 0 or 7 or <= 10) and not 15}");
object o = 2;
WriteLine($"o is int and >= 1: {o is int and >= 1}");
root@e68a47087ad8:/MyConsoleApp# dotnet run
x is >= 5 and < 10: True
x is (<= 0 or 7 or <= 10) and not 15: True
o is int and >= 1: True
Wie ich später bemerkte, wurde der Quellcode jedes Mal, wenn ich ihn änderte, nur mit "dotnet run" ohne "dotnet build" erstellt. Praktisch. Danke fürs Lesen.
https://qiita.com/okazuki/items/8043ea6f0095b3019cdf#target-typed-new-%E5%BC%8F ↩︎
Recommended Posts