• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

linux dotnet

date: 2024-07-07 excerpt: linux dotnetの使い方

tag: dotnetlinux


linux dotnetの使い方

インストール

ubuntu

$ sudo apt-get update
$ sudo apt-get install -y dotnet-sdk-8.0
$ export PATH=$PATH:/usr/share/dotnet/

使用例

新規プロジェクト作成

$ dotnet new console -o MyNewApp

パッケージの追加(NuGet)

$ dotnet add package Newtonsoft.Json

ビルド

$ dotnet build

実行

$ dotnet run

コード

using System;
using Newtonsoft.Json;


class Program
{
    static void Main(string[] args)
    {
        Console.Write("Enter your name: ");
        string? name = Console.ReadLine();
        if (string.IsNullOrWhiteSpace(name))
        {
            Console.WriteLine("You must enter a name.");
        }
        else
        {
            Console.WriteLine($"Hello, {name}!");
        }

        var person = new { Name = name, Age = 25 };
        string json = JsonConvert.SerializeObject(person);
        Console.WriteLine(json);

        var deserializedPerson = JsonConvert.DeserializeObject(json);
        Console.WriteLine(deserializedPerson);
    }
}

参考

  • Windows、Linux、および macOS に .NET をインストールする - Microsoft Docs


dotnetlinux Share Tweet