LoginSignup
5
13

More than 5 years have passed since last update.

VisualStudioCode使い始め

Last updated at Posted at 2017-11-04

この記事は、VisualStudioCodeを使用してC#開発を行うための環境整備を目的としています。

環境についてUbuntu17.10を使用しています。

使い方

コンソール表示

「Ctrl+Alt+@」キーでコンソールを表示できます。
図のように、「ターミナル」タブではWindowsではコマンドプロンプト、Linuxではシェルが使用できます。

Screenshot_Console.png

覗き見機能(Peek)

どのような定義がされているか定義箇所のソースコードを表示中のタブ上で確認できます。

確認したいシンボルにカーソル位置を合わせて、shift+F12キーを入力すると、
下記図のように参照元と定義元のソースコードが表示されます。

Screenshot_Nozokimi.png

プロジェクト(サンプルプロジェクト作成)

カレントディレクトリに「Study」フォルダを作成し、その中にStudy01プロジェクトを作成します。
コンソールアプリケーション用のプロジェクトを作成するため、「console」を指定します。

~/Projects/VscStudy$ dotnet new console -n Study01 -o Study

Welcome to .NET Core!
---------------------
Learn more about .NET Core @ https://aka.ms/dotnet-docs. Use dotnet --help to see available commands or go to https://aka.ms/dotnet-cli-docs.

Telemetry
--------------
The .NET Core tools collect usage data in order to improve your experience. The data is anonymous and does not include command-line arguments. The data is collected by Microsoft and shared with the community.
You can opt out of telemetry by setting a DOTNET_CLI_TELEMETRY_OPTOUT environment variable to 1 using your favorite shell.
You can read more about .NET Core tools telemetry @ https://aka.ms/dotnet-cli-telemetry.
Getting ready...
The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on Study/Study01.csproj...
  Restoring packages for /home/atachi/Projects/VscStudy/Study/Study01.csproj...
  Generating MSBuild file /home/atachi/Projects/VscStudy/Study/obj/Study01.csproj.nuget.g.props.
  Generating MSBuild file /home/atachi/Projects/VscStudy/Study/obj/Study01.csproj.nuget.g.targets.
  Restore completed in 132.66 ms for /home/atachi/Projects/VscStudy/Study/Study01.csproj.


Restore succeeded.

ソースコードの編集

テンプレートから作成したプロジェクトには、下記のソースが含まれています。
まずは、そのままビルドを行い実行ファイルの作成とアプリの実行を行います。

Program.cs
using System;

namespace Study01
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

NuGetパッケージ利用方法

プロジェクトがNuGetパッケージを使う場合は、csprojファイルに使用するパッケージの情報を記載します。

今回はNewtonsoft.Jsonを使用します。

下記のようにItemGroup要素内にPackageReference要素を追加し使用するライブラリを記載します。

Study01.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3"/>
  </ItemGroup>
</Project>

nugetパッケージのリストア

csprojファイルに記述したライブラリを取得するには、csprojファイルと同じフォルダ内で、dotnet restoreコマンドを実行します。

このコマンドは、プロジェクト内に対象のライブラリを取得していない場合に使用するコマンドで、例えばgitなどでcsprojプロジェクトをクローンしてきた場合でも、このコマンドを実行してNuGetパッケージの取得を行います。

~/Projects/VscStudy/Study$ dotnet restore
  Restoring packages for /home/atachi/Projects/VscStudy/Study/Study01.csproj...
  Installing Newtonsoft.Json 10.0.3.
  Restore completed in 2.1 sec for /home/atachi/Projects/VscStudy/Study/Study01.csp
roj.

csprojファイルの依存ライブラリを編集するたびに、このコマンドを実行しネットワークからパッケージをダウンロードします。

ソリューション

VisualStudioでは、複数のプロジェクトをまとめたソリューションという概念があり、複数のアセンブリをまとめる役割を担います。

VisualStudioCodeにおいても同様の概念を持つソリューションを作成することができます。

大抵のプロジェクトチームでは、ソースツリー構成でソリューションファイルはすべてのプロジェクトの1つ上のフォルダ階層に配置することが多いです。

この例では、下記のような位置関係とするソリューションを作成します。

~/Projects/VscStudy$ tree -L 2
.
├── MyStudy.sln
└── Study
    ├── bin
    ├── obj
    ├── Program.cs
    └── Study01.csproj

3 directories, 3 files

ソリューション操作

~/Projects/VscStudy/Study$ ls
bin  obj  Program.cs  Study01.csproj
~/Projects/VscStudy/Study$ cd ..
~/Projects/VscStudy$ ls
Study
~/Projects/VscStudy$ dotnet new sln -n MyStudy
The template "Solution File" was created successfully.
~/Projects/VscStudy$ ls
MyStudy.sln  Study
~/Projects/VscStudy$ ls Study/
bin/            obj/            Program.cs      Study01.csproj  .vscode/
~/Projects/VscStudy$ dotnet sln add Study/Study01.csproj
Project `Study/Study01.csproj` added to the solution.

開発方法

ビルドと実行

VisualStudioCodeのコンソールで、カレントフォルダをStudy01.csprojがあるフォルダへ移動し、コマンドを実行します。

~/Projects/VscStudy/Study$ ls
bin  obj  Program.cs  Study01.csproj
~/Projects/VscStudy/Study$ dotnet run
Hello World!
~/Projects/VscStudy/Study$

デバッグ

VisualStudioCodeの画面左側に表示されている
image.png

アイコンをクリックすると、デバッグ用の表示に切り替わります。

Screenshot_Debug.png

image.png
をクリックしデバッグを開始します。

プラグイン

C#機能

Microsoft公式の拡張機能で、VisualStudioCodeでC#開発向けの環境を提供している。

インストール方法

マーケットプレイスから、「csharp☆Microsoft」(注:☆は半角スペース)

image.png

Code Outline

現バージョン(1.17.2)には、表示中のソースコードのアウトラインを表示する機能はありませんので、拡張機能を使用してアウトラインを表示します。

この拡張機能を導入すると、図のようにエクスプローラーペインに「CODE OUTLINE」が表示されます。
CODE OUTLINEでは現在アクティブなタブのソースコードについてアウトラインを表示します。

Screenshot_ext_CodeOutline.png

C# XML Documentation Comments

VisualStudioのように、クラス定義やインターフェース定義、メソッド定義にXMLDocumentのテンプレートを自動的に挿入できる拡張機能です。

各定義の上の行で、「///」を入力すると自動的にXMLDocumentのテンプレートが挿入される。
メソッドの場合は、自動的に引数を解析しXMLDocumentを追加します。

Screenshot_ext_xmldocumentcomments.png

C# Extensions(まとめ中)

C#開発において便利な機能を提供します。
機能が多いので、現在まとめています。

ToDo Parser

ソースコード中の「ToDo」コメントを検索し、出力ウィンドウに表示します。

この拡張機能の便利な点として、ToDo出力コマンドがマウスで実行できる点にあります。
ステータスバーに、ToDo表示コマンドアイコンが表示されており、このアイコンをクリックするだけで出力ウィンドウが表示され、ToDo一覧が出力されます。

.NET Core導入(のログ)

$ curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   983  100   983    0     0    983      0  0:00:01 --:--:--  0:00:01  3120
$ sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
$ sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-artful-prod artful main" > /etc/apt/sources.list.d/dotnetdev.list'
$ sudo apt update
ヒット:1 http://jp.archive.ubuntu.com/ubuntu artful InRelease
取得:2 http://jp.archive.ubuntu.com/ubuntu artful-updates InRelease [76.7 kB]  
無視:3 http://dl.google.com/linux/chrome/deb stable InRelease                  
ヒット:4 http://jp.archive.ubuntu.com/ubuntu artful-backports InRelease        
取得:5 http://packages.microsoft.com/repos/vscode stable InRelease [2,801 B]   
ヒット:6 http://dl.google.com/linux/chrome/deb stable Release                  
取得:7 http://jp.archive.ubuntu.com/ubuntu artful-updates/main amd64 Packages [22.3 kB]
取得:8 https://packages.microsoft.com/repos/microsoft-ubuntu-artful-prod artful InRelease [2,846 B]
取得:9 http://jp.archive.ubuntu.com/ubuntu artful-updates/main i386 Packages [22.4 kB]
取得:10 http://jp.archive.ubuntu.com/ubuntu artful-updates/main amd64 DEP-11 Metadata [8,272 B]
取得:11 http://jp.archive.ubuntu.com/ubuntu artful-updates/universe amd64 Packages [10.4 kB]
取得:12 http://jp.archive.ubuntu.com/ubuntu artful-updates/universe i386 Packages [10.4 kB]
取得:13 http://jp.archive.ubuntu.com/ubuntu artful-updates/universe amd64 DEP-11 Metadata [2,188 B]
取得:14 http://packages.microsoft.com/repos/vscode stable/main amd64 Packages [31.0 kB]
取得:16 http://security.ubuntu.com/ubuntu artful-security InRelease [70.3 kB]  
取得:17 https://packages.microsoft.com/repos/microsoft-ubuntu-artful-prod artful/main amd64 Packages [2,349 B]
無視:18 http://ppa.launchpad.net/jonathonf/vlc/ubuntu artful InRelease         
無視:19 http://ppa.launchpad.net/ne0sight/chrome-gnome-shell/ubuntu artful InRelease
取得:20 http://security.ubuntu.com/ubuntu artful-security/universe amd64 DEP-11 Metadata [2,184 B]
ヒット:21 http://ppa.launchpad.net/webupd8team/java/ubuntu artful InRelease
エラー:22 http://ppa.launchpad.net/jonathonf/vlc/ubuntu artful Release         
  404  Not Found
エラー:23 http://ppa.launchpad.net/ne0sight/chrome-gnome-shell/ubuntu artful Release
  404  Not Found
パッケージリストを読み込んでいます... 完了
E: リポジトリ http://ppa.launchpad.net/jonathonf/vlc/ubuntu artful Release には Release ファイルがありません。
N: このようなリポジトリから更新を安全に行うことができないので、デフォルトでは更新が無効になっています。
N: リポジトリの作成とユーザ設定の詳細は、apt-secure(8) man ページを参照してください。
E: リポジトリ http://ppa.launchpad.net/ne0sight/chrome-gnome-shell/ubuntu artful Release には Release ファイルがありません。
N: このようなリポジトリから更新を安全に行うことができないので、デフォルトでは更新が無効になっています。
N: リポジトリの作成とユーザ設定の詳細は、apt-secure(8) man ページを参照してください。
$ sudo apt-get install dotnet-sdk-2.0.2
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
以下の追加パッケージがインストールされます:
  aspnetcore-store-2.0.0 dotnet-host dotnet-hostfxr-2.0.0 dotnet-runtime-2.0.0
  liblttng-ust-ctl2 liblttng-ust0 libunwind8 liburcu6
以下のパッケージが新たにインストールされます:
  aspnetcore-store-2.0.0 dotnet-host dotnet-hostfxr-2.0.0 dotnet-runtime-2.0.0
  dotnet-sdk-2.0.2 liblttng-ust-ctl2 liblttng-ust0 libunwind8 liburcu6
アップグレード: 0 個、新規インストール: 9 個、削除: 0 個、保留: 3 個。
107 MB のアーカイブを取得する必要があります。
この操作後に追加で 311 MB のディスク容量が消費されます。
続行しますか? [Y/n] Y
取得:1 http://jp.archive.ubuntu.com/ubuntu artful/main amd64 liburcu6 amd64 0.10.0-2 [51.7 kB]
取得:2 http://jp.archive.ubuntu.com/ubuntu artful/universe amd64 liblttng-ust-ctl2 amd64 2.9.1-1build2 [79.3 kB]
取得:3 http://jp.archive.ubuntu.com/ubuntu artful/universe amd64 liblttng-ust0 amd64 2.9.1-1build2 [152 kB]
取得:4 http://jp.archive.ubuntu.com/ubuntu artful/main amd64 libunwind8 amd64 1.1-4.1ubuntu2 [46.3 kB]
取得:5 https://packages.microsoft.com/repos/microsoft-ubuntu-artful-prod artful/main amd64 aspnetcore-store-2.0.0 amd64 2.0.0-1 [18.0 MB]
取得:6 https://packages.microsoft.com/repos/microsoft-ubuntu-artful-prod artful/main amd64 dotnet-host amd64 2.0.0-1 [32.9 kB]
取得:7 https://packages.microsoft.com/repos/microsoft-ubuntu-artful-prod artful/main amd64 dotnet-hostfxr-2.0.0 amd64 2.0.0-1 [134 kB]
取得:8 https://packages.microsoft.com/repos/microsoft-ubuntu-artful-prod artful/main amd64 dotnet-runtime-2.0.0 amd64 2.0.0-1 [18.0 MB]
取得:9 https://packages.microsoft.com/repos/microsoft-ubuntu-artful-prod artful/main amd64 dotnet-sdk-2.0.2 amd64 2.0.2-1 [70.5 MB]
107 MB を 1分 27秒 で取得しました (1,223 kB/s)                                 
以前に未選択のパッケージ aspnetcore-store-2.0.0 を選択しています。
(データベースを読み込んでいます ... 現在 145354 個のファイルとディレクトリがインストールされています。)
.../0-aspnetcore-store-2.0.0_2.0.0-1_amd64.deb を展開する準備をしています ...
aspnetcore-store-2.0.0 (2.0.0-1) を展開しています...
以前に未選択のパッケージ dotnet-host を選択しています。
.../1-dotnet-host_2.0.0-1_amd64.deb を展開する準備をしています ...
dotnet-host (2.0.0-1) を展開しています...
以前に未選択のパッケージ dotnet-hostfxr-2.0.0 を選択しています。
.../2-dotnet-hostfxr-2.0.0_2.0.0-1_amd64.deb を展開する準備をしています ...
dotnet-hostfxr-2.0.0 (2.0.0-1) を展開しています...
以前に未選択のパッケージ liburcu6:amd64 を選択しています。
.../3-liburcu6_0.10.0-2_amd64.deb を展開する準備をしています ...
liburcu6:amd64 (0.10.0-2) を展開しています...
以前に未選択のパッケージ liblttng-ust-ctl2:amd64 を選択しています。
.../4-liblttng-ust-ctl2_2.9.1-1build2_amd64.deb を展開する準備をしています ...
liblttng-ust-ctl2:amd64 (2.9.1-1build2) を展開しています...
以前に未選択のパッケージ liblttng-ust0:amd64 を選択しています。
.../5-liblttng-ust0_2.9.1-1build2_amd64.deb を展開する準備をしています ...
liblttng-ust0:amd64 (2.9.1-1build2) を展開しています...
以前に未選択のパッケージ libunwind8 を選択しています。
.../6-libunwind8_1.1-4.1ubuntu2_amd64.deb を展開する準備をしています ...
libunwind8 (1.1-4.1ubuntu2) を展開しています...
以前に未選択のパッケージ dotnet-runtime-2.0.0 を選択しています。
.../7-dotnet-runtime-2.0.0_2.0.0-1_amd64.deb を展開する準備をしています ...
dotnet-runtime-2.0.0 (2.0.0-1) を展開しています...
以前に未選択のパッケージ dotnet-sdk-2.0.2 を選択しています。
.../8-dotnet-sdk-2.0.2_2.0.2-1_amd64.deb を展開する準備をしています ...
dotnet-sdk-2.0.2 (2.0.2-1) を展開しています...
liblttng-ust-ctl2:amd64 (2.9.1-1build2) を設定しています ...
dotnet-host (2.0.0-1) を設定しています ...
aspnetcore-store-2.0.0 (2.0.0-1) を設定しています ...
liburcu6:amd64 (0.10.0-2) を設定しています ...
libunwind8 (1.1-4.1ubuntu2) を設定しています ...
libc-bin (2.26-0ubuntu2) のトリガを処理しています ...
man-db (2.7.6.1-2) のトリガを処理しています ...
liblttng-ust0:amd64 (2.9.1-1build2) を設定しています ...
dotnet-hostfxr-2.0.0 (2.0.0-1) を設定しています ...
dotnet-runtime-2.0.0 (2.0.0-1) を設定しています ...
dotnet-sdk-2.0.2 (2.0.2-1) を設定しています ...
This software may collect information about you and your use of the software, and send that to Microsoft.
Please visit http://aka.ms/dotnet-cli-eula for more information.
Welcome to .NET Core!
---------------------
Learn more about .NET Core @ https://aka.ms/dotnet-docs. Use dotnet --help to see available commands or go to https://aka.ms/dotnet-cli-docs.

.NET Core Tools Telemetry
--------------
The .NET Core Tools include a telemetry feature that collects usage information. It is important that the .NET Team understands how the tools are being used so that we can improve them.

The data collected is anonymous and will be published in an aggregated form for use by both Microsoft and community engineers under the Creative Commons Attribution License.

The .NET Core Tools telemetry feature is enabled by default. You can opt-out of the telemetry feature by setting an environment variable DOTNET_CLI_TELEMETRY_OPTOUT (for example, 'export' on macOS/Linux, 'set' on Windows) to true (for example, 'true', 1). You can read more about .NET Core tools telemetry at https://aka.ms/dotnet-cli-telemetry.

Installation Note
--------------
A command will be run during the install process that will improve project restore speed and enable offline access. It will take up to a minute to complete.
libc-bin (2.26-0ubuntu2) のトリガを処理しています ... 
5
13
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
5
13