C#入門サイト

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="description" content="C# オールインワン入門サイト">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>オールインワン C# 入門サイト</title>

  <!-- ★ スタイル(CSS)をまとめて定義 ★ -->
  <style>
    /* ページ全体 */
    body {
      margin: 0;
      padding: 0;
      font-family: sans-serif;
      line-height: 1.6;
      background: #f9f9f9;
    }

    /* ヘッダー */
    header {
      background: #f0f0f0;
      padding: 10px;
    }

    header h1 {
      margin: 0;
      font-size: 1.5em;
    }

    /* ナビゲーション */
    nav ul {
      list-style: none;
      padding: 0;
      display: flex;
      gap: 10px;
      margin-top: 5px;
    }

    nav a {
      text-decoration: none;
      color: #333;
      font-weight: bold;
    }

    nav a:hover {
      text-decoration: underline;
    }

    /* メインコンテンツ */
    main {
      max-width: 900px;
      margin: 20px auto;
      padding: 20px;
      background: #fff;
      box-shadow: 0 0 4px rgba(0,0,0,0.1);
    }

    main h2 {
      margin-top: 0;
      border-left: 6px solid #333;
      padding-left: 8px;
      margin-bottom: 15px;
    }
    main h3 {
      margin-bottom: 5px;
    }

    /* コードブロック */
    pre {
      background: #fafafa;
      padding: 10px;
      overflow-x: auto;
    }
    code {
      font-family: Consolas, monospace;
    }

    /* フォームなど(クイズ用) */
    form section {
      margin-bottom: 20px;
    }
    #result {
      margin-top: 20px;
      background: #eef;
      padding: 10px;
      border: 1px solid #ccf;
      display: inline-block;
    }

    /* フッター */
    footer {
      text-align: center;
      padding: 10px;
      background: #f0f0f0;
      margin-top: 20px;
    }

    /* セクション間の余白調整 */
    section {
      margin-bottom: 40px;
    }
  </style>
</head>
<body>

  <!-- ★ ヘッダーとナビゲーション ★ -->
  <header>
    <h1>オールインワン C# 入門サイト</h1>
    <nav>
      <ul>
        <li><a href="#top">トップ</a></li>
        <li><a href="#environment">環境構築 &amp; .NET</a></li>
        <li><a href="#basics">C#の基本</a></li>
        <li><a href="#syntax">基本文法</a></li>
        <li><a href="#control-flow">条件分岐・ループ</a></li>
        <li><a href="#oop">オブジェクト指向</a></li>
        <li><a href="#advanced">応用トピック</a></li>
        <li><a href="#debugging">デバッグ &amp; テスト</a></li>
        <li><a href="#quiz">クイズ</a></li>
        <li><a href="#example">サンプル</a></li>
      </ul>
    </nav>
  </header>

  <!-- ★ メインコンテンツ ★ -->
  <main>
    <!-- トップ (id="top") -->
    <section id="top">
      <h2>このサイトについて</h2>
      <p>
        ここでは、C# を初めて学ぶ方や、基礎を復習したい方向けに、  
        C#と.NETの概要から開発環境構築、文法、オブジェクト指向、さらにデバッグやテスト、  
        一歩進んだ応用トピック(Generics, LINQ, 非同期など)まで幅広くカバーしています。  
      </p>
      <p>
        ページ上部のナビゲーションから各セクションへ移動できます。  
        一通り学習した後は、クイズに挑戦したり、サンプルプログラムを動かしてみましょう。
      </p>
    </section>

    <!-- 環境構築 (id="environment") -->
    <section id="environment">
      <h2>環境構築 &amp; .NET</h2>
      <article>
        <h3>.NETエコシステムの概要</h3>
        <p>
          C# は .NET 上で動作する言語です。  
          近年はクロスプラットフォーム対応の「.NET (Core)」が主流であり、  
          Windows だけでなく Mac や Linux でも利用可能です。
        </p>
      </article>
      <article>
        <h3>Windows での開発</h3>
        <p>
          <strong>Visual Studio</strong> (Community 版) または <strong>VS Code</strong> が一般的です。  
          <code>dotnet</code> CLI を使ってプロジェクト作成・ビルド・実行が可能です。
        </p>
<pre><code>
// 例: コンソールアプリプロジェクトの作成
dotnet new console -o MyApp
cd MyApp
dotnet run
</code></pre>
      </article>
      <article>
        <h3>Mac / Linux での開発</h3>
        <p>
          公式サイトから .NET SDK をインストールすることで、同様に C# を利用できます。  
          VS Code + C#拡張機能があればブレークポイントによるデバッグも可能です。
        </p>
      </article>
    </section>

    <!-- C#の基本 (id="basics") -->
    <section id="basics">
      <h2>C#の基本</h2>
      <article>
        <h3>C#とは?</h3>
        <p>
          C# (シーシャープ) は、マイクロソフトが開発したオブジェクト指向言語です。  
          Javaに似た構文を持ち、GenericsやLINQ、非同期などモダンな機能を数多く備えています。
        </p>
      </article>
      <article>
        <h3>Hello World</h3>
<pre><code class="language-csharp">
using System;

namespace HelloWorldApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
</code></pre>
        <p>このように、<code>Main</code> メソッドがエントリポイントとして呼び出されます。</p>
      </article>
    </section>

    <!-- 基本文法 (id="syntax") -->
    <section id="syntax">
      <h2>基本文法</h2>
      <article>
        <h3>変数とデータ型</h3>
<pre><code class="language-csharp">
int number = 10;
double pi = 3.14;
bool isActive = true;
string message = "Hello C#";
</code></pre>
        <p>
          C# は強い型付け言語であり、<code>var</code> キーワードで型推論も可能です。
        </p>
      </article>
      <article>
        <h3>演算子</h3>
        <p>
          算術演算子、比較演算子、論理演算子などを使用できます。
        </p>
<pre><code class="language-csharp">
int x = 5;
int y = 3;
Console.WriteLine(x + y); // 8
Console.WriteLine(x > y); // true
</code></pre>
      </article>
    </section>

    <!-- 条件分岐・ループ (id="control-flow") -->
    <section id="control-flow">
      <h2>条件分岐・ループ</h2>
      <article>
        <h3>if / else / else if</h3>
<pre><code class="language-csharp">
int score = 85;
if(score >= 80) {
    Console.WriteLine("Excellent!");
} else if(score >= 60) {
    Console.WriteLine("Good!");
} else {
    Console.WriteLine("Keep trying!");
}
</code></pre>
      </article>
      <article>
        <h3>switch</h3>
<pre><code class="language-csharp">
int dayOfWeek = 2;
switch(dayOfWeek)
{
    case 0:
        Console.WriteLine("日曜日");
        break;
    case 1:
        Console.WriteLine("月曜日");
        break;
    case 2:
        Console.WriteLine("火曜日");
        break;
    default:
        Console.WriteLine("不明な曜日");
        break;
}
</code></pre>
      </article>
      <article>
        <h3>for / while / do-while</h3>
<pre><code class="language-csharp">
// forループ
for(int i = 0; i < 5; i++) {
    Console.WriteLine(i);
}

// whileループ
int j = 0;
while(j < 5) {
    Console.WriteLine(j);
    j++;
}

// do-whileループ
int k = 0;
do {
    Console.WriteLine(k);
    k++;
} while(k < 5);
</code></pre>
      </article>
    </section>

    <!-- オブジェクト指向 (id="oop") -->
    <section id="oop">
      <h2>オブジェクト指向</h2>
      <article>
        <h3>クラスとオブジェクト</h3>
<pre><code class="language-csharp">
class Person
{
    public string Name;
    public int Age;

    public void Greet()
    {
        Console.WriteLine($"こんにちは、{Name}です。");
    }
}

class Program
{
    static void Main()
    {
        Person p = new Person();
        p.Name = "Taro";
        p.Age = 20;
        p.Greet();
    }
}
</code></pre>
      </article>
      <article>
        <h3>継承とポリモーフィズム</h3>
<pre><code class="language-csharp">
class Animal
{
    public string Name { get; set; }
    public virtual void Speak()
    {
        Console.WriteLine("何かを話す");
    }
}

class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("ワンワン");
    }
}
</code></pre>
      </article>
      <article>
        <h3>インターフェイス</h3>
<pre><code class="language-csharp">
interface IFlyable
{
    void Fly();
}

class Bird : IFlyable
{
    public void Fly()
    {
        Console.WriteLine("羽ばたいて飛ぶ");
    }
}
</code></pre>
      </article>
    </section>

    <!-- 応用トピック (id="advanced") -->
    <section id="advanced">
      <h2>応用トピック</h2>
      <article>
        <h3>Generics (ジェネリクス)</h3>
<pre><code class="language-csharp">
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);

class Box<T>
{
    public T Value { get; set; }
    public Box(T value)
    {
        Value = value;
    }
}
</code></pre>
      </article>
      <article>
        <h3>デリゲート・イベント</h3>
<pre><code class="language-csharp">
public delegate void MyDelegate(string msg);

public class Publisher
{
    public event MyDelegate OnPublish;

    public void Publish(string msg)
    {
        OnPublish?.Invoke(msg);
    }
}
</code></pre>
      </article>
      <article>
        <h3>LINQ</h3>
<pre><code class="language-csharp">
int[] data = {1,2,3,4,5,6};
var evens = from x in data
            where x % 2 == 0
            select x;
</code></pre>
      </article>
      <article>
        <h3>非同期 (async/await)</h3>
<pre><code class="language-csharp">
static async Task Main()
{
    Console.WriteLine("開始");
    await Task.Delay(1000);
    Console.WriteLine("終了");
}
</code></pre>
      </article>
    </section>

    <!-- デバッグ & テスト (id="debugging") -->
    <section id="debugging">
      <h2>デバッグ &amp; テスト</h2>
      <article>
        <h3>Visual Studioでのデバッグ</h3>
        <ol>
          <li>行番号の左をクリックしてブレークポイントを設定</li>
          <li>「デバッグ実行」ボタンで開始</li>
          <li>停止したらローカル変数やステップ実行を確認</li>
        </ol>
      </article>
      <article>
        <h3>例外処理</h3>
<pre><code class="language-csharp">
try
{
    int num = int.Parse("abc");
}
catch(FormatException fe)
{
    Console.WriteLine("フォーマットエラー: " + fe.Message);
}
finally
{
    Console.WriteLine("終了処理");
}
</code></pre>
      </article>
      <article>
        <h3>ユニットテスト</h3>
<pre><code class="language-csharp">
using Xunit;

public class CalcTests
{
    [Fact]
    public void AddTest()
    {
        int result = Calc.Add(2, 3);
        Assert.Equal(5, result);
    }
}

public static class Calc
{
    public static int Add(int x, int y) => x + y;
}
</code></pre>
        <p>
          xUnit、NUnit、MSTest などが有名です。<code>dotnet test</code> でテスト実行できます。
        </p>
      </article>
    </section>

    <!-- クイズ (id="quiz") -->
    <section id="quiz">
      <h2>C#クイズ</h2>
      <p>ラジオボタンで答えを選んで「採点する」ボタンを押してください。</p>

      <form id="quizForm">
        <article>
          <h3>Q1. C# コンソールアプリのエントリポイントは?</h3>
          <label>
            <input type="radio" name="q1" value="A">
            public static void start()
          </label><br>
          <label>
            <input type="radio" name="q1" value="B">
            private void main()
          </label><br>
          <label>
            <input type="radio" name="q1" value="C">
            static void Main(string[] args)
          </label><br>
          <label>
            <input type="radio" name="q1" value="D">
            run()
          </label>
        </article>

        <article>
          <h3>Q2. 次のうち整数型ではないのは?</h3>
          <label>
            <input type="radio" name="q2" value="A">
            int
          </label><br>
          <label>
            <input type="radio" name="q2" value="B">
            double
          </label><br>
          <label>
            <input type="radio" name="q2" value="C">
            long
          </label><br>
          <label>
            <input type="radio" name="q2" value="D">
            short
          </label>
        </article>

        <article>
          <h3>Q3. C# の例外処理に使われるキーワードの組み合わせは?</h3>
          <label>
            <input type="radio" name="q3" value="A">
            try / catch / else
          </label><br>
          <label>
            <input type="radio" name="q3" value="B">
            try / check / throw
          </label><br>
          <label>
            <input type="radio" name="q3" value="C">
            try / catch / finally
          </label><br>
          <label>
            <input type="radio" name="q3" value="D">
            try / except / ensure
          </label>
        </article>

        <button type="button" onclick="gradeQuiz()">採点する</button>
      </form>

      <!-- 採点結果を表示する領域 -->
      <div id="result"></div>
    </section>

    <!-- サンプル (id="example") -->
    <section id="example">
      <h2>サンプルプログラム</h2>

      <article>
        <h3>1. ユーザー入力を受け取る</h3>
<pre><code class="language-csharp">
using System;

namespace SampleApp
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("名前を入力してください:");
            string name = Console.ReadLine();
            Console.WriteLine($"こんにちは、{name}さん!");
        }
    }
}
</code></pre>
      </article>

      <article>
        <h3>2. 例外処理</h3>
<pre><code class="language-csharp">
using System;

namespace SampleApp
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("整数を入力してください:");
            try
            {
                int number = int.Parse(Console.ReadLine());
                Console.WriteLine("二乗:" + (number * number));
            }
            catch(Exception e)
            {
                Console.WriteLine("エラー:" + e.Message);
            }
        }
    }
}
</code></pre>
      </article>

      <article>
        <h3>3. LINQ を使ったフィルタリング</h3>
<pre><code class="language-csharp">
using System;
using System.Linq;
using System.Collections.Generic;

namespace SampleApp
{
    class Program
    {
        static void Main()
        {
            List<int> numbers = new List<int> { 1, 4, 7, 2, 9, 12 };
            var evenNumbers = numbers.Where(x => x % 2 == 0);

            Console.WriteLine("偶数のみ抽出:");
            foreach(var n in evenNumbers)
            {
                Console.WriteLine(n);
            }
        }
    }
}
</code></pre>
      </article>

      <article>
        <h3>4. 非同期処理</h3>
<pre><code class="language-csharp">
using System;
using System.Threading.Tasks;

namespace SampleApp
{
    class Program
    {
        static async Task Main()
        {
            Console.WriteLine("開始");
            await Task.Delay(2000);
            Console.WriteLine("2秒後に終了");
        }
    }
}
</code></pre>
      </article>
    </section>

  </main>

  <!-- フッター -->
  <footer>
    <p>&copy; 2025 C# Tutorial Site</p>
  </footer>

  <!-- ★ クイズ判定用JavaScriptをまとめて定義 ★ -->
  <script>
    // 正解のマッピング(q1,q2,q3,...)
    const correctAnswers = {
      q1: "C", // static void Main(string[] args)
      q2: "B", // doubleは整数型ではない
      q3: "C"  // try / catch / finally
    };

    function gradeQuiz() {
      const form = document.getElementById("quizForm");
      let score = 0;
      let total = Object.keys(correctAnswers).length;

      // 各問題について、ラジオボタンの選択値をチェック
      for(const [question, answer] of Object.entries(correctAnswers)) {
        const userAnswer = form.elements[question].value;
        if(userAnswer === answer) {
          score++;
        }
      }

      // 結果表示
      const resultDiv = document.getElementById("result");
      resultDiv.innerHTML = `
        <h3>採点結果: ${score} / ${total} 正解</h3>
        <p>${getComment(score, total)}</p>
      `;
    }

    // 点数に応じたコメントを返す
    function getComment(score, total) {
      if(score === total) {
        return "パーフェクト!素晴らしいです!";
      } else if(score >= total - 1) {
        return "惜しい!あともう少し!";
      } else {
        return "まだまだ勉強が必要です。がんばりましょう!";
      }
    }
  </script>

</body>
</html>

投稿者: chosuke

趣味はゲームやアニメや漫画などです

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です