LoginSignup
6
4

More than 1 year has passed since last update.

M1 MacでC#を使いたい (.NET 6.0)

Posted at

はじめに

Visual Studio for Macの最新版(2022プレビュー版)を入れたらなんとかなるのかなと思ったのだけど、すんなりいかなかったのでメモ。
色々調べているうちに以下の記事に辿り着いて、動くようになりました。

Apple シリコン コンピューターでの Visual Studio for Mac 17.0 Preview と .NET

私と同じようにうまくいかなかった人は、上記ページで紹介されているスクリプトで、一度.NETをアンインストールしてから、上記ページにリンクがある.NET 6 Arm64 SDKをインストールするといけると思います。

とりあえず .NET 6.0のSDKを入れてしまえば、以下のやり方でVisual StudioがなくてもC#が使えるようになります。

Hello Worldの作成と実行

コンソールで適当なディレクトリ(今回はhelloとしました)を作って、カレントディレクトリを移動してから、dotnet new consoleでテンプレートアプリが作成されます。

% dotnet new console
テンプレート "コンソール アプリ" が正常に作成されました。

作成後の操作を処理しています...
/Users/xxxxx/Projects/hello/hello.csproj で ' dotnet restore ' を実行しています...
  復元対象のプロジェクトを決定しています...
  /Users/xxxxx/Projects/hello/hello.csproj を復元しました (120 ms)。
正常に復元されました。

lsで生成物を確認します。

% ls
Program.cs  hello.csproj    obj

objディレクトリも作られていましたが、ディレクトリごと消してもビルドのときに自動で作られたので、実質Program.cshello.csprojの2つだけです。
Program.csの中身はこんな感じで非常にシンプルです。

Program.cs
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

従来は以下のようなコードだったのですが、.NET 6.0からはMain()関数の中身だけでいけるようになったそうです。
最近Pythonの楽さに慣れてきたので、こんなにシンプルに書けるのはありがたいです。

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;

namespace MyApp // Note: actual namespace depends on the project name.
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

ビルド&実行は dotnet runコマンドで。

% dotnet run
Hello, World!

ビルドのあとはこんな感じになりました。

% tree
.
├── Program.cs
├── bin
│   └── Debug
│       └── net6.0
│           ├── hello
│           ├── hello.deps.json
│           ├── hello.dll
│           ├── hello.pdb
│           ├── hello.runtimeconfig.json
│           └── ref
│               └── hello.dll
├── hello.csproj
├── hello.sln
└── obj
    ├── Debug
    │   └── net6.0
    │       ├── apphost
    │       ├── hello.AssemblyInfo.cs
    │       ├── hello.AssemblyInfoInputs.cache
    │       ├── hello.GeneratedMSBuildEditorConfig.editorconfig
    │       ├── hello.GlobalUsings.g.cs
    │       ├── hello.assets.cache
    │       ├── hello.csproj.AssemblyReference.cache
    │       ├── hello.csproj.CoreCompileInputs.cache
    │       ├── hello.csproj.FileListAbsolute.txt
    │       ├── hello.dll
    │       ├── hello.genruntimeconfig.cache
    │       ├── hello.pdb
    │       └── ref
    │           └── hello.dll
    ├── hello.csproj.nuget.dgspec.json
    ├── hello.csproj.nuget.g.props
    ├── hello.csproj.nuget.g.targets
    ├── project.assets.json
    └── project.nuget.cache

bin/Debug/net6.0/helloがビルドされた実行モジュールのようです。
直接実行もできました。

% bin/Debug/net6.0/hello
Hello, World!

Visual Studio for Mac について

Visual Studio 2019だと .NET 6.0はサポートされていないようで、こんなエラーが出ました。

/usr/local/share/dotnet/sdk/6.0.100/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.DefaultItems.targets(5,5):
 Warning NETSDK1182:
 Visual Studio 2019 で .NET 6.0 をターゲットにすることはサポートされていません。
 (NETSDK1182) (linqtest)

というか、そもそも、Visual Studio for Mac 2022プレビュー版を入れたつもりだったのだけど、なぜか2019に変化していたんですよね。なんか更新したときにダウングレードされてしまったような感じ。

Visual Studio Community 2019 for Mac
Version 8.10.14 (build 17)

でよくわからないので Visual Studio for Macをゴミ箱に入れてから、再度 Visual Studio for Mac 2022プレビュー版を入れたらちゃんと動きました。

Visual Studio Community 2022 for Mac Preview
Version 17.0 Preview (17.0 build 4729)

ただ、C#でコンソールアプリ書くだけなら、上のSDKとVisual Studio Codeだけで十分かも知れません。
Visual Studio CodeにC#拡張を入れてデバッグもできました。

まとめ

  • Visual Studio 2022 for Macプレビュー版を入れた後に、.NETを削除してから、.NET 6.0 Arm64版のSDKを入れる
  • Hello Worldはdotnet new consoledotnet runだけでOK
  • .NET 6.0からはMain関数の中身だけでプログラムが書ける!

次は前回の記事で用意したSQL Server(というかAzure SQL Edge)に接続するのをやってみる予定。

余談

昔は.NET Coreと呼んでたと思うのだけど、去年くらいからCoreの名前が外れたんですね。
.NET Core - 出典: フリー百科事典『ウィキペディア(Wikipedia)』

6
4
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
6
4