Enumerable.Range

LINQ系列

 這是專門產生範圍內的數字序列。  

            IEnumerable<int> num =Enumerable.Range(1,10);
            string numList = string.Join(",", num);
            Console.WriteLine(numList);
            //1,2,3,4,5,6,7,8,9,10
            num = Enumerable.Range(2, 10);
             numList = string.Join(",", num);
            Console.WriteLine(numList);
            //2,3,4,5,6,7,8,9,10,11
            IEnumerable<char> eng = Enumerable.Range(0, 26)
                .Select(x => (char) (x + 'a'));
            string engList = string.Join(",", eng);
            Console.WriteLine(engList);
            //a,b,c,d,e,f,g,....z
            //隨機取十碼
            Random rand = new Random(Guid.NewGuid().GetHashCode());
            List<int> nums = new List<int>(Enumerable.Range(1, 100));
            nums = nums.OrderBy(n => rand.Next()).ToList<int>();
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(nums[i].ToString());
            }

 

 

元哥的筆記