前提
- .NET Core SDK 2.0 ~ がインストールされている
-
Omnisharp がインストールされている
- ※ Omnisharp は vscode の拡張機能から入れられます。
サンプルプロジェクトの作成
vscode で最新の C# 7.3 を使用するために必要なことは
.csproj
の編集omnisharp.path
の編集
です。
サンプルのプロジェクトを作って順番に見ていきます。
PS > mkdir LatestCsharp
PS > cd ./LatestCsharp
PS > dotnet new console
PS > dotnet build
vscode で LatestCsharp
フォルダを開く。
Required assets to build and debug are missing from 'LatestCsharp'. Add them?
=> Yes を選択。
<project_name>.csproj
の設定
統合開発環境 Vsiual Stuido を使っている場合には、Visual Studio UI で変更できますが、
vscodeの場合は<project_name>.csproj
を直接編集する必要があります。
<PropertyGroup>
の子要素に<LangVersion>7.3</LangVersion>
を追加します。
プロジェクトによって<PropertyGroup>
がない場合は追加してください。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
+ <LangVersion>7.3</LangVersion>
</PropertyGroup>
</Project>
omnisharp.path
の編集
vscode のUserSetting.json
を開きます。
Windows の場合は Ctrl
+ ,
で開けます。
"omnisharp.path": "latest"
を追加します。
{
"omnisharp.path": "latest"
}
一度ビルドしておきます。
PS > dotnet build
最新の Omnisharp がインストールされます。
Getting latest OmniSharp version information
Downloading package 'Latest OmniSharp Version Information' (xxx KB).................... Done!
Installing OmniSharp Version = 1.32.2-beta.33...
Platform: win32, x86_64
Finished
C# 7.3の機能を試す
C# 7.3 の機能を使って正常に動作するか確かめます。
C# 7.3 からタプル同士を ==
!=
で比較できるようになったので、
そちらで確かめてみます。
Program.cs
を以下のように編集します。
using System;
namespace LatestCsharp {
class Program {
static void Main(string[] args) {
var monthdayA = (month: 8, day: 04);
var monthdayB = (month: 8, day: 04);
Console.WriteLine($"{nameof(monthdayA)} == {nameof(monthdayB)} is {monthdayA == monthdayB}");
}
}
}
run
コマンドで実際に動かすと、ビルドが通って以下のように表示されるはずです。
PS > dotnet run
monthdayA == monthdayB is True
タプル同士の比較は上記の場合、以下のように展開されるそうです。
monthdayA.month == monthdayB.month && monthdayA.day == monthdayB.day