L'autre jour, .NET 5 est sorti. [^ dotnet5] .NET 5 prend en charge C # 9. J'aimerais l'essayer tout de suite, mais je ne veux pas l'installer sur mon PC, mais je veux le toucher un peu, alors j'aimerais utiliser Docker pour préparer un environnement de développement .NET 5 jetable.
Essayez Play avec Docker [^ pwd], un environnement d'exécution Docker jetable sur le cloud qui peut être utilisé avec des navigateurs ne nécessitant pas l'installation de Docker.
L'environnement Linux de Play with Docker lorsqu'il est exécuté est Alpine Linux 3.12
, et la version de Docker est 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
Exécutez l'image Docker du SDK .NET 5.
Avec l'option -it
, connectez l'entrée / sortie standard au shell et embarquez le conteneur du SDK .NET 5 tel quel.
--rm
, le conteneur est automatiquement supprimé lorsque le conteneur se termine [^ --rm].
Si vous l'essayez sur votre PC, il ne restera plus de conteneurs inutiles.$ 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:/#
Vous pouvez voir que .NET 5 est installé.
root@e68a47087ad8:/# dotnet --version
5.0.100
La distribution Linux semble être Debian.
root@e68a47087ad8:/# more /etc/issue
Debian GNU/Linux 10 \n \l
Je voudrais créer une application console pour essayer les nouvelles fonctionnalités de C # 9.
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.
Le projet a été créé.
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
Je veux éditer le fichier, mais vim n'est pas inclus, donc je voudrais l'installer. Tout d'abord, mettez à jour le package.
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.
Installez 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) ...
Maintenant que vous êtes prêt, essayez le code C # 9. Pour plus de détails, veuillez consulter l'article suivant auquel j'ai fait référence.
Au point d'entrée, vous pouvez écrire le code directement sans classe. Cela semble pratique pour les lots simples.
root@e68a47087ad8:/MyConsoleApp# vim Program.cs
System.Console.WriteLine("Hello from top.");
Construire. (Omis dans les exemples suivants.)
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.
Vous pouvez facilement créer des objets immuables.
De plus, si les valeurs de propriété correspondent, ==
sera true
même pour les types d'enregistrement avec des références différentes.
Vous pouvez également utiliser l'expression with
pour créer un nouvel enregistrement avec seulement quelques propriétés réécrites, ce que je n'ai pas essayé dans cet article.
La création d'objets de valeur semble être rapide.
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]
Vous pouvez omettre le nom de type nouveau
lorsqu'il est clair par inférence de type.
Jusqu'à présent, «var» était déduit du côté droit, mais il ne pouvait pas être utilisé dans les variables membres.
De plus, il semble que le nom du type puisse être omis même si le type est clair dans l'argument ou la valeur de retour.
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
«Ou», «et», «not» ont été ajoutés à l'expression conditionnelle. En tant que fonctionnalité, il est évalué une fois, il semble donc facile à utiliser pour comparer les valeurs de retour des expressions avec des effets secondaires. (= Il n'est plus nécessaire de stocker les résultats dans des variables temporaires, puis de les comparer pour que les effets secondaires ne se produisent pas plusieurs fois) En outre, il semble qu'il puisse être écrit de manière concise avec la correspondance de motifs.
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
Comme je l'ai remarqué plus tard, chaque fois que j'ai changé le code source, il a été construit simplement par «dotnet run» sans «dotnet build». Pratique. Merci pour la lecture.
https://qiita.com/okazuki/items/8043ea6f0095b3019cdf#target-typed-new-%E5%BC%8F ↩︎
Recommended Posts