5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

c# プロパティの歴史(ざっくり)

Last updated at Posted at 2019-08-13

はじめに

現在(2019年8月現在)に至るまで、プロパティがどういうふうに進化していったか、ざっくりまとめてみた。

c#1.0、1.2、2.0時代

c#には初めから「プロパティ」という機能があり、
SetterメソッドとGetterメソッドいちいち作るより楽&見やすい。
↓↓こんなふうにいちいちメソッド作らなくても。。

    private int _a;
    public int GetA()
    {
        return _a;
    }
    pubic void SetA(int v)
    {
        _a = v
    }

↓↓簡単に書けるように!

    private int _a;
    public int A
    {
        get { return _a; }
        set { _a = value; }
    }

c#3~5時代

自動実装が追加されてさらに便利に!!
単純に代入取得だけの処理を書きたい場合は、わざわざgetとsetの中身を書かなくてもよくなった。

public int X { get; set; }    // まさかの一行!!

c#6時代

便利機能がいろいろ追加された!

たとえば、初期化子というものがつかえたり↓↓

public int A { get; set; } = 3;
// 「= 3」の部分が初期化子。
// ここでは、初期値3を入れている

自動実装で、Getterのみ指定ができるように。

public int A{ get; }
// コンストラクタか、初期化詞を遣えば、1度だけ設定できる。
// 以後値が変わることはない。

c#7時代

「{ return }」の記載が、「=>」により省略できるようになった。
(※c#6自体にこの機能はあったが、プロパティには使えなかった。)
↓↓こんな感じにかけるようになった。

public int A => 3;
// 「get {return 3;}」と同じ意味。

参考

++C++ // 未確認飛行 C
https://ufcpp.net/study/csharp/ap_ver6.html
buildinsider
https://www.buildinsider.net/language/csharplang/0600
C# 7.0 の新機能
https://qiita.com/tadnakam/items/afe390679e8b5dfa7fa3

5
2
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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?