8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MacでC#コンソールアプリ作成

Last updated at Posted at 2017-08-03

前置き

WindowsじゃないとC#開発が十全にできないなぁと思っていたけれど、
Macでも dotnet コマンドを使えばいい感じに開発できそうだったので手順メモ。

基本手順

dotnet commandのインストール

プロジェクトのフォルダ作成。

$ mkdir ConsoleSample
$ cd ConsoleSample

Solutionを作成。

Solutionは、iOSのworkspace, androidのrootprojectに対応するプロジェクトをまとめるプロジェクト。

$ dotnet new sln

プロジェクト本体を作成。

$ mkdir App
$ cd App
$ dotnet new console
$ cd ..

ここまででこんな構成になっているはず。

$ tree
.
├── App
│   ├── App.csproj
│   └── Program.cs
└── ConsoleSample.sln

dotnet newは色々なテンプレートがあるのだけど、ここではソリューションとコンソールアプリのテンプレを利用した。

consoleテンプレによって下記のようなProgramファイルが作成されているはず。

App/Program.cs
using System;

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

SolutionにAppプロジェクトを依存追加する。

$ dotnet sln ConsoleSample.sln add App/App.csproj

restore & buildする

$ dotnet restore
$ dotnet build

実行

$ dotnet run --project App/App.csproj
Hello World!

ライブラリ追加

Json.NETを追加してみる.

$ dotnet add App/App.csproj package Newtonsoft.Json
App/Program.cs
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {
            var dictionary = new Dictionary<string, string>() {{"message", "Hello World!"}};
            var serialized = JsonConvert.SerializeObject(dictionary);
            Console.WriteLine(serialized);
        }
    }
}

実行

$ dotnet run --project App/App.csproj
{"message":"Hello World!"}

今までMacのC#開発はだいぶつらみがあったけど、dotnetコマンド登場のおかげで楽に開発できそうです。 :smile:

その他

作成したサンプル

8
2
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
8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?