Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9707

MacのVisual Studio CodeでC#を実行する

$
0
0

Visual Studio CodeでC#を実行できるようにしようとするといろいろ嵌まったので,記録しておきたいと思います.

Visual Studio Codeをインストール

https://code.visualstudio.com
dmgをダウンロードして,appをコピーすることでインストール
スクリーンショット 2020-04-20 15.31.22.png

ターミナル作業

以下はターミナルでの作業になります.

Homebrewをインストール

本家サイトに掲載されているとおり,以下のコードを入力して実行します.

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

途中でパスワードを求められる(恐らく要管理者権限).

ScriptCSをインストール

$ brew install scriptcs

monoのパッケージなどもここで読み込まれている.

Visual Studio Codeの拡張機能を登録

スクリーンショット 2020-04-20 15.31.39.png

検索メニューからCode Runnerを検索して,選ぶ.

スクリーンショット 2020-04-20 15.32.46.png

「install」ボタンをクリックしてインストールする.

その他,日本語メニューの方がいい人は「Japanese Language Pack」も入れておく.

Code Runnerの設定

拡張機能の右下のギヤアイコンを右クリックして「拡張機能の設定」を開く
スクリーンショット 2020-04-21 11.57.58.png
スクリーンショット 2020-04-21 12.02.48.png

clearPreviousOutputやrunInTerminal,saveFileBeforeRunなどはチェックしておいた方がいいかもしれません.

「settings.jsonで編集」と書かれているリンクを開いて以下のコードを追加

"code-runner.executorMap": { "csharp": "scriptcs -script" },

この設定がないと"Unexpected named argument:"となって,嵌まります.
StackOverflowの記事

settings.jsonは,私の場合には上記でチェックしたものを含めて,こんな風になっていました.
スクリーンショット 2020-04-21 12.07.27.png

ファイルを保存して,設定完了.

テスト実行

C#コード

helloworld.cs
usingSystem;classtest{publicstaticvoidMain(){Console.WriteLine("Hello,World");}}test.Main();
(base) Mac:~ user$ scriptcs -script "/Users/user/Desktop/helloworld.cs"
Hello,World
(base) Mac:~ user$ 
echoback.cs
usingSystem;classtest{publicstaticvoidMain(){stringa=Console.ReadLine();Console.WriteLine(a);}}test.Main();
(base) Mac:~ user$ scriptcs -script "/Users/user/Desktop/echoback.cs"
こんにちは
こんにちは
(base) Mac:~ user$ 

末尾のtest.Main()はCodeRunner.appのC#環境などでは不要ですが,この環境では書かないと動かないので注意しないといけません.


Viewing all articles
Browse latest Browse all 9707

Trending Articles