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#ショートプログラミング





TryParseでTryブロック追放 - C#ショートプログラミング -

OLD
class Program
    {
        static void Main(string[] args)
        {
            string src = "123";
            try
            {
                System.Diagnostics.Debug.WriteLine(int.Parse(src));
            }
            catch (Exception)
            {
                System.Diagnostics.Debug.WriteLine(-1);
            }
        }
    }

NEW
class Program
    {
        static void Main(string[] args)
        {
            string src = "123";
            int result;
            System.Diagnostics.Debug.WriteLine(int.TryParse(src, out result) ? result : -1);
        }
    }

Result

123

・例外処理は重い処理なので、できるだけ利用は避けるのが定石


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





条件指定付き繰り返し - C#ショートプログラミング -

OLD

class Program
    {   
        static void Main()
        {
            string[] a = { null, "タロウ", null, "ハナコ" };
            foreach (var n in a)
            {
                if (n != null) { 
                    System.Diagnostics.Debug.WriteLine(n + "さん");
                }
            }
        }
    }

NEW

using System.Linq;

    class Program
    {
        static void Main()
        {
            string[] a = { null, "タロウ", null, "ハナコ" };
            foreach (var n in a.Where((c) => c != null))
            {
                if (n != null)
                {
                    System.Diagnostics.Debug.WriteLine(n + "さん");
                }
            }
        }
    }

Result

タロウさん
ハナコさん

・ループ内if文を追放できる

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





foreachをselectメソッドでカウント - C#ショートプログラミング -


OLD
class Program
    {
        static void Main()
        {
            string[] a = { "タロウ", "ハナコ", "ジロウ" };
            int count = 1;
            foreach (var n in a)
            {
                System.Diagnostics.Debug.WriteLine(n + "さんは" + (count) + "位");
                count++;

            }
        }
    }

NEW
class Program
    {
        //C#3.0
        static void Main()
        {
            string[] a = { "タロウ", "ハナコ", "ジロウ" };
            foreach (var n in a.Select((s, i) => new { i, s }))
            {
                System.Diagnostics.Debug.WriteLine(n.s + "さんは" + (n.i + 1) + "位");
            }
        }
    }


Result

タロウさんは1位
ハナコさんは2位
ジロウさんは3位

・読み上げるだけのcount変数を排除できる
・匿名オブジェクトを生成するので回数が多い場合オーバーヘッドになるかも

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