3
3

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#のプロパティ

Posted at

C#はメンバ変数の代わりにプロパティを定義しやすくするために、プロパティの自動実装機能が追加されているので、publicメンバ変数は定義するべきではなく、独自のget関数やset関数を用意することもよくないらしい。

書き方

private int hoge;
public int Hoge
{
    get { return hoge; }
    set { hoge = value; }
}

privateメンバ変数を省いた形

public int Hoge{get;set;}

関数扱いらしいので仮想関数にもできる

public virtual int Hoge{get;set;}

片方だけアクセス修飾子をかえたりもできる

public int Hoge{
    get;
    protected set;
}
3
3
2

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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?