2014年6月1日日曜日

条件判定の結果を変数に入れる - C#ショートプログラミング -


OLD
class Program
    {
        private static bool myMethod(int x)
        {
            if (x > 100)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        static void Main(string[] args)
        {
            System.Diagnostics.Debug.WriteLine(myMethod(100));
            System.Diagnostics.Debug.WriteLine(myMethod(101));
        }
    }

NEW
class Program
    {
        private static bool myMethod(int x){
            return x > 100;
        }

        static void Main(string[] args)
        {
            System.Diagnostics.Debug.WriteLine(myMethod(100));
            System.Diagnostics.Debug.WriteLine(myMethod(101));
        }
    }

Result

False
True

・条件判断式はbool型でtrue, falseの値を持つ
なので結果をそのまま返却できる


引用元:
C#ショートプログラミング





0 件のコメント:

コメントを投稿