###独自クラスを使い、Listに追加する方法がわかりません。
下記のように、既にあるlistを独自クラスを使ったlistへの変換で、
可能な限り、if、foreachなど、分岐や繰り返しで処理したいのですが、
良い方法はないでしょうか。
###ソースコード
class Person { public string Name; public string Gender; public Person(string name, string gender) { this.Name = name; this.Gender = gender; } } class Program { static void Main(string[] args) { // 既にあるlist List<string> list = new List<string>(); list.Add("E 女"); list.Add("F 女"); list.Add("G 男"); list.Add("H 男"); list.Add("A 女"); list.Add("B 男"); list.Add("C 女"); list.Add("D 男"); //独自クラスを使用したlistへ List<Person> People = new List<Person>(); foreach (string s1 in list) { if (s1.Contains(list[0])) { People.Add(new Person("E", "女")); } else if (s1.Contains(list[1])) { People.Add(new Person("F", "女")); } else if (s1.Contains(list[2])) { People.Add(new Person("G", "男")); } else if (s1.Contains(list[3])) { People.Add(new Person("H", "男")); } else if (s1.Contains(list[4])) { People.Add(new Person("A", "女")); } else if (s1.Contains(list[5])) { People.Add(new Person("B", "男")); } else if (s1.Contains(list[6])) { People.Add(new Person("C", "女")); } else if (s1.Contains(list[7])) { People.Add(new Person("D", "男")); } } } }
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。