0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#プログラミング言語: 初心者から上級者まで

Posted at

はじめに

C#(シーシャープ)は、マイクロソフトが開発した強力で汎用性の高いプログラミング言語です。ゲーム開発、デスクトップアプリケーション、ウェブアプリケーションなど、幅広い用途に使用されています。

第1章: C#の基礎

C#は.NETフレームワークの一部であり、共通言語ランタイム(CLR)上で動作します[1]。以下は、基本的なHello Worldプログラムです:

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("こんにちは、世界!");
    }
}

このプログラムは、コンソールに「こんにちは、世界!」と表示します。

第2章: 変数と定数

C#では、変数を使用してデータを格納します。定数は変更できない値を表します[1]。

int age = 25;  // 変数
const double PI = 3.14159;  // 定数

Console.WriteLine($"年齢: {age}");
Console.WriteLine($"円周率: {PI}");

第3章: データ型

C#には、整数、浮動小数点数、文字列、ブール値など、さまざまなデータ型があります[1]。

int number = 10;
double price = 9.99;
string name = "田中太郎";
bool isStudent = true;

Console.WriteLine($"数値: {number}, 価格: {price}, 名前: {name}, 学生: {isStudent}");

第4章: 制御構造

if文、switch文、ループなどの制御構造を使用して、プログラムの流れを制御します[3]。

int score = 85;

if (score >= 80)
{
    Console.WriteLine("優秀です!");
}
else if (score >= 60)
{
    Console.WriteLine("合格です。");
}
else
{
    Console.WriteLine("頑張りましょう。");
}

第5章: 配列とコレクション

配列とコレクションを使用して、複数の値を格納および管理します[3]。

string[] fruits = { "りんご", "バナナ", "オレンジ" };

foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);
}

第6章: メソッド

メソッドを使用して、コードを再利用可能な部分に分割します[3]。

static int Add(int a, int b)
{
    return a + b;
}

int result = Add(5, 3);
Console.WriteLine($"5 + 3 = {result}");

第7章: クラスとオブジェクト

クラスを使用してオブジェクトを作成し、データとメソッドをカプセル化します[3]。

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

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

Person person = new Person { Name = "鈴木花子", Age = 30 };
person.Introduce();

第8章: 継承とポリモーフィズム

継承を使用してクラス階層を作成し、ポリモーフィズムを使用して柔軟性を高めます。

class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("動物が鳴きます");
    }
}

class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("ワンワン!");
    }
}

Animal animal = new Dog();
animal.MakeSound();  // "ワンワン!"と出力

第9章: インターフェースと抽象クラス

インターフェースと抽象クラスを使用して、共通の動作を定義します。

interface IFlying
{
    void Fly();
}

abstract class Bird
{
    public abstract void Sing();
}

class Sparrow : Bird, IFlying
{
    public override void Sing()
    {
        Console.WriteLine("チュンチュン");
    }

    public void Fly()
    {
        Console.WriteLine("スズメが飛びます");
    }
}

第10章: 例外処理

try-catchブロックを使用して、エラーを適切に処理します[3]。

try
{
    int result = 10 / 0;  // ゼロ除算エラー
}
catch (DivideByZeroException e)
{
    Console.WriteLine("エラー: ゼロで除算することはできません。");
}
finally
{
    Console.WriteLine("処理が完了しました。");
}

第11章: デリゲートとイベント

デリゲートとイベントを使用して、柔軟なコールバックメカニズムを実装します。

delegate void MessageHandler(string message);

class Notifier
{
    public event MessageHandler MessageReceived;

    public void SendMessage(string message)
    {
        MessageReceived?.Invoke(message);
    }
}

Notifier notifier = new Notifier();
notifier.MessageReceived += (msg) => Console.WriteLine($"受信: {msg}");
notifier.SendMessage("こんにちは!");

第12章: LINQ (Language Integrated Query)

LINQを使用して、データの操作とクエリを簡単に行います。

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var evenNumbers = numbers.Where(n => n % 2 == 0);
foreach (var num in evenNumbers)
{
    Console.WriteLine(num);
}

第13章: 非同期プログラミング

async/awaitキーワードを使用して、非同期プログラミングを実装します。

async Task<string> FetchDataAsync()
{
    await Task.Delay(2000);  // 2秒待機
    return "データが取得されました";
}

async Task Main()
{
    Console.WriteLine("データを取得中...");
    string result = await FetchDataAsync();
    Console.WriteLine(result);
}

第14章: ジェネリクス

ジェネリクスを使用して、型安全性とコードの再利用性を向上させます。

class Stack<T>
{
    private List<T> items = new List<T>();

    public void Push(T item)
    {
        items.Add(item);
    }

    public T Pop()
    {
        if (items.Count == 0)
            throw new InvalidOperationException("スタックが空です");

        T item = items[items.Count - 1];
        items.RemoveAt(items.Count - 1);
        return item;
    }
}

Stack<int> intStack = new Stack<int>();
intStack.Push(1);
intStack.Push(2);
Console.WriteLine(intStack.Pop());  // 2

第15章: ファイル操作

System.IOネームスペースを使用して、ファイルの読み書きを行います。

using System.IO;

string content = "これはファイルに書き込まれるテキストです。";
File.WriteAllText("example.txt", content);

string readContent = File.ReadAllText("example.txt");
Console.WriteLine(readContent);

以上が、C#プログラミング言語の基本から応用までをカバーする15章構成の記事です。各章で重要な概念を説明し、実践的なコード例を提供しています。C#の学習を進める上で、この記事が役立つことを願っています。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?