Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

IEnumerable.ToListメソッド

Enumerable.ToList(TSource) メソッド (System.Linq)
IEnumerable から List を作成します。

ソースコード

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqSample {
    class Program {
        static void Main(string[] args) {
            Man[] mans = new Man[] { 
                new Man() { Name = "suzuki taro", Age = 20 },
                new Man() { Name = "sato taro", Age = 21 },
                new Man() { Name = "sato jiro", Age = 20 }
            };

            List<Man> list = mans.ToList();

            foreach (Man man in list) {
                Console.WriteLine(man);
            }

            Console.ReadKey();
        }
    }

    class Man {
        public string Name { get; set; }
        public int Age { get; set; }

        public override string ToString() {
            return string.Format("Name = {0}, Age = {1}", Name, Age);
        }
    }
}

実行結果