1
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#】クラスってなに?

Last updated at Posted at 2025-10-03

クラスとは?

クラスは設計図のようなものです。
例えば「車」という設計図があれば、その設計図から「トヨタの車」「日産の車」など、実際の車(オブジェクト)を作ることができます。

  • クラス = 設計図
  • オブジェクト = 設計図から作った実体

C#でのクラスの書き方

「Car」というクラスを定義を定義すると

public class Car
{
    public string Color;
    public void Drive()
    {
        Console.WriteLine("走ります!");
    }
}
  • Color → 車の色(データ、つまり変数/フィールド
  • Drive() → 車を走らせる動き(処理、つまりメソッド

クラスからオブジェクトを作る

クラスを定義したら、実際に使ってみる。

Car myCar = new Car();
myCar.Color = "赤";
myCar.Drive(); // 走ります!

このとき

  • myCarオブジェクト(実体)
  • Car はその設計図

まとめ

  • クラス = 設計図
  • オブジェクト = 設計図から作ったもの
  • クラスには「データ(変数)」と「動き(メソッド)」をまとめられる

続き

1
1
1

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