Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

foreach, switch, LINQ

Discussion in 'Programming Questions and Suggestions' started by CyberVic, Jan 2, 2015.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. CyberVic Apprentice Engineer

    Messages:
    106
    These are the top things I've noticed need to be there. At the very least the code below should compile and should run.

    Test 1 and 3 compile and work.
    Test 2 compiles, but doesn't run.
    Test 4 doesn't compile.
    Switch statements (like the one in main) cause it not to compile at all.

    I know that foreach and LINQ / Lambda expressions are known issues, but not sure why switch statements aren't appearing to work.


    Code:
    <div><div>        void Main()
            {
                int test = 1;
     
                switch (test)
                {
                    case 1: test1(); break;
                    case 2: test2(); break;
                    case 3: test3(); break;
                    default: test1(); break;
                }
            }
     
            void test1()
            {
                var items = new List&lt;int>();
                items.Add(100);
                items.Add(25);
                items.Add(3);
     
                int sum = 0;
                for (int i = 0; i &lt; items.Count; i++)
                {
                    sum += items[i];
                }
            }
     
            void test2()
            {
                var items = new List&lt;int>();
                items.Add(100);
                items.Add(25);
                items.Add(3);
     
                int sum = 0;
                foreach (int item in items)
                {
                    sum += item;
                }
            }
     
            void test3()
            {
                var items = new List&lt;int>();
     
                int sum = 0;
                while (sum &lt;= 100)
                {
                    sum += 10;
                }
            }
     
            void test4()
            {
                var items = new List&lt;int>();
                items.Add(100);
                items.Add(25);
                items.Add(3);
     
                var q = from item in items
                        where item > 10
                        select item;
     
                q.Sum();
            }
    </div>
    </div>
     
Thread Status:
This last post in this thread was made more than 31 days old.