LoginSignup
12
12

More than 5 years have passed since last update.

C# > 参照型(クラス)のリストと配列の初期化と列挙 その2

Last updated at Posted at 2015-11-22

その2の経緯

前回の記事において、ToniTakekawaさんからいろいろ教えて頂き
ました。ありがとうございました。
教えて頂いた事を元に自分なりに改良してみました。
改良の目的は表示用の文字列を一気に列挙する事です。

気が付いたこと

String.Join()メソッドがstaticメソッドであり、後置記法できないことです。例えば、(文字列型配列).Join("\n")みたいな。拡張メソッドが本来はLINQを実現させる為のものだとどこかで読んだことがあるので従来の基本クラスには追加されなかったのでしょう。
しかし、従来の基本クラスを使う時、ちょいちょい後置記法したい場面がでてくるんですよね~。
片っ端から後置記法できるように自分で拡張メソッドを作ればいいだけの話なんですけどね。やっぱり、String.Join("\n", T[])より、T[].Join("\n")と書きたい(笑)。

ソースプログラム

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
            // プリミティブなListの初期化
            var intList = new List<int>() { 1, 2, 3 };
            // ちなみにvarを使わない場合はこちら
            // List<int> intList = new List<int> { 1, 2, 3 };
            // 以下のように宣言する事はできない(配列とは違う)
            // List<int> intList = { 1, 2, 3 };

            // プリミティブな配列の初期化
            var intList2 = new int[] { 4, 5, 6 };
            // ちなみにvarを使わない場合はこちら
            // int[] intList2 = { 4, 5, 6 };

            // 参照型のListの初期化
            var employees = new List<Employee>()
            {
                new Employee {Id = 1, Name = "ジョン" },
                new Employee {Id = 2, Name = "ジャック" }
            };

            // 参照型の配列の初期化
            var employees2 = new Employee[]
            {
                new Employee {Id = 3, Name = "ジョアンナ" },
                new Employee {Id = 4, Name = "ジュリア" }
            };

            // 表示用文字列の列挙を定義
            var intListStrings = intList.Select(i => $"item={i}");
            var intList2Strings = intList2.Select(i => $"item={i}");
            var employeesStrings = employees.Select(o => $"Id={o.Id}, Name={o.Name}");
            var employees2Strings = employees2.Select(o => $"Id={o.Id}, Name={o.Name}");

            // 更にこれらの定義をひとつの定義にまとめる
            var resultStrings = new[]
            {
                intListStrings,
                intList2Strings,
                employeesStrings,
                employees2Strings
            }.SelectMany(e => e);

            // で、最後に一気に表示する
            resultStrings.ForEach(Console.WriteLine);
            // ちなみにForEach拡張メソッドを使用しない場合はこちら
            // Console.WriteLine(string.Join("\n", resultStrings));

            // 結果を確認する為のお約束
            Console.WriteLine("\n\nPush any key!");
            Console.ReadKey();
        }
    }
}
IEnumerableExtension.cs
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication9
{
    public static class IEnumerableExtension
    {
        public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
        {
            foreach (var x in source)
            {
                action(x);
            }
        }
    }
}
Employee.cs
namespace ConsoleApplication9
{
    public class Employee
    {
        public int Id { set; get; }
        public string Name { set; get; }
    }
}
12
12
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12