LoginSignup
15
14

More than 5 years have passed since last update.

Visual Studio Code で最新の C# (C# 7.3) を使う方法

Posted at

前提

  • .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

vscodeLatestCsharpフォルダを開く。

:warning: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 の編集

vscodeUserSetting.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を以下のように編集します。

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

参考: C# によるプログラミング入門 > C# 7.3 の新機能 > タプルの ==, != 比較

15
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14