Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

C# 公式 API リファレンスっぽくなるサマリーの書き味について考える

0
Posted at

はじめに

Microsoft Learn の API リファレンスっぽくなる書き方について考えてみました。

改訂履歴

  • 2026/08/15 : 初版公開。

本文

1. 環境

  • .NET 8.0
  • Visual Studio Community 2022

2. 実装

2-1. C#

サンプルとして IPerson とそれを継承する User クラスを作りました。

IPerson.cs
namespace SummaryExample.ClassLibrary;

/// <summary>
/// 人を表します。
/// </summary>
public interface IPerson
{
    /// <summary>
    /// 名前を取得します。
    /// </summary>
    public string Name { get; }

    /// <summary>
    /// 年齢を取得または設定します。
    /// </summary>
    public int Age { get; set; }
}
User.cs
namespace SummaryExample.ClassLibrary;

/// <summary>
/// ユーザーを表します。
/// </summary>
public class User : IPerson
{
    /// <summary>
    /// <see cref="User"/> クラスの新しいインスタンスを初期化します。
    /// </summary>
    /// <param name="name">ユーザー名。</param>
    /// <param name="age">ユーザーの年齢。</param>
    /// <param name="isActive">ユーザーがアクティブかどうかを示す値。</param>
    /// <param name="registeredAt">ユーザーの登録日時。</param>
    public User(string name, int age, bool isActive, DateTime registeredAt)
    {
        Name = name;
        Age = age;
        IsActive = isActive;
        RegisteredAt = registeredAt;
    }

    /// <inheritdoc/>
    public string Name { get; }

    /// <inheritdoc/>
    public int Age { get; set; }

    /// <summary>
    /// ユーザーがアクティブかどうかを示す値を取得または設定します。
    /// </summary>
    public bool IsActive { get; set; }

    /// <summary>
    /// ユーザーの登録日時を取得または設定します。
    /// </summary>
    public DateTime RegisteredAt { get; set; }

    /// <summary>
    /// ユーザー情報を表示します。
    /// </summary>
    public void Display() { }

    /// <summary>
    /// ユーザーが成人かどうかを判断します。
    /// </summary>
    /// <returns>ユーザーが成人の場合は <see langword="true"/>、それ以外の場合は <see langword="false"/>。</returns>
    public bool IsAdult()
    {
        return Age >= 18;
    }
}

2-2. WPF

エントリポイントから考えていきます。Application クラスと OnStartup メソッドには、公式 API リファレンスがあるので、参考になります。

App.xaml.cs
using System.Windows;

namespace SummaryExample.Wpf;

/// <summary>
/// Windows Presentation Foundation アプリケーションを表します。
/// </summary>
public partial class App : Application
{
    /// <summary>
    /// <see cref="Application.Startup"/> イベントを処理します。
    /// </summary>
    /// <param name="e">イベントデータを含む <see cref="StartupEventArgs"/>。</param>
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        var mainWindow = new MainWindow();
        MainWindow = mainWindow;
        mainWindow.Show();
    }
}

次はビューモデルです。MVVM ライブラリには、個人的にデファクトスタンダードだと思っている CommunityToolkit.Mvvm を使用しています。

MainViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace SummaryExample.Wpf;

/// <summary>
/// メイン画面の状態管理と操作を提供するビューモデル。
/// </summary>
public partial class MainViewModel : ObservableObject
{
    [ObservableProperty]
    private string _title = string.Empty;

    /// <summary>
    /// 設定画面を開きます。
    /// </summary>
    [RelayCommand]
    private void OpenSettings() { }
}

2-3. Windows Forms

Windows Forms でも、Application クラスの公式 API リファレンスがあります。

Program.cs
namespace SummaryExample.Forms;

/// <summary>
/// アプリケーションのエントリポイントを定義します。
/// </summary>
internal static class Program
{
    /// <summary>
    /// アプリケーションのエントリポイントです。
    /// </summary>
    [STAThread]
    private static void Main()
    {
        ApplicationConfiguration.Initialize();
        Application.Run(new MainForm());
    }
}

次はフォームクラスです。イベントやイベントハンドラーの公式 API リファレンスを参考にしながら、考えていきます。

MainForm.cs
namespace SummaryExample.Forms;

/// <summary>
/// メイン画面を表すフォーム。
/// </summary>
public partial class MainForm : Form
{
    /// <summary>
    /// <see cref="MainForm"/> クラスの新しいインスタンスを初期化します。
    /// </summary>
    public MainForm()
    {
        InitializeComponent();
    }

    /// <summary>
    /// <see cref="MainForm"/> の <see cref="Form.Load"/> イベントを処理します。
    /// </summary>
    /// <param name="sender">イベントのソース。</param>
    /// <param name="e">イベントデータを含まないオブジェクト。</param>
    private void MainForm_Load(object sender, EventArgs e) { }

    /// <summary>
    /// <see cref="MainForm"/> の <see cref="Form.FormClosed"/> イベントを処理します。
    /// </summary>
    /// <param name="sender">イベントのソース。</param>
    /// <param name="e">イベントデータを含む <see cref="FormClosedEventArgs"/>。</param>
    private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { }

    /// <summary>
    /// <see cref="OpenSettingsButton"/> の <see cref="Control.Click"/> イベントを処理します。
    /// </summary>
    /// <param name="sender">イベントのソース。</param>
    /// <param name="e">イベントデータを含まないオブジェクト。</param>
    private void OpenSettingsButton_Click(object sender, EventArgs e) { }
}

2-4. ASP.NET

今回は MVC のテンプレートにしました。プロジェクトの作成時「最上位レベルのステートメントを使用しない」にチェックを入れると Program.cs が生成されます。

Program.cs
namespace SummaryExample.AspDotNetMvc;

/// <summary>
/// アプリケーションのエントリポイントを定義します。
/// </summary>
public class Program
{
    /// <summary>
    /// アプリケーションのエントリポイントです。
    /// </summary>
    public static void Main(string[] args)
    {
        WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
        builder.Services.AddControllersWithViews();
        WebApplication app = builder.Build();

        if (!app.Environment.IsDevelopment())
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthorization();
        app.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
        app.Run();
    }
}

次はコントローラークラスです。テンプレートで自動生成される HomeController で考えます。なお Controller には公式 API リファレンスがあります。

ILogger パラメーターの説明の実例は以下にありました。

HomeController.cs
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using SummaryExample.AspDotNetMvc.Models;

namespace SummaryExample.AspDotNetMvc.Controllers;

/// <summary>
/// アプリケーションのホーム画面を管理する MVC コントローラー。
/// </summary>
public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;

    /// <summary>
    /// <see cref="HomeController"/> クラスの新しいインスタンスを初期化します。
    /// </summary>
    /// <param name="logger">ロガー。</param>
    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    /// <summary>
    /// ホーム画面のビューを返します。
    /// </summary>
    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// プライバシーポリシー画面のビューを返します。
    /// </summary>
    public IActionResult Privacy()
    {
        return View();
    }

    /// <summary>
    /// エラー画面のビューを返します。
    /// </summary>
    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}

おわりに

自分一人で作るときの、ちょっとしたこだわりってやつです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?