4
4

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# EnumとSwitchは相性が良い、可読性の話

Last updated at Posted at 2015-06-03

あまりこれ関係の記述をネット上で見なかったので。

例えば Enumをこのように記述します。

Enumの記述
        public enum Target
        {
            OFF,
            ON,
            NONE,
            COMPLETE
        }

これらの判定をSwitchでする場合、数値で判定するよりも、この記述が可読性も高く、見易いです。

Switchの記述
switch (Target)
            {
                case Target.OFF:
                    ///OFFの場合の動作を記述
                    return;
                case Target.ON:
                    ///ONの場合の動作を記述
                    break;
                case Target.NONE:
                    ///NONEの場合の動作を記述
                    break;
                case Target.COMPLETE:
                    ///COMPLETEの場合の動作を記述
                    break;
                default:
                    return;
            }
4
4
3

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?