Created
June 1, 2017 05:12
-
-
Save divide-by-zero/6c70c2eabcceb291d922b23ee691e2d8 to your computer and use it in GitHub Desktop.
Genericsメソッドとparamsの優先順位 ref: http://qiita.com/divideby_zero/items/504f6db139b4f580fd9c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class DebugUtil | |
| { | |
| public static void ListDump<T>(IEnumerable<T> datas) | |
| { | |
| foreach (var data in datas) | |
| { | |
| Console.WriteLine(data); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static void Main(string[] args) | |
| { | |
| var data = new[] {1, 2, 3, 4, 5}; | |
| var list = new List<string>(); | |
| list.Add("Hello"); | |
| list.Add("Nice"); | |
| list.Add("Too"); | |
| list.Add("Meet"); | |
| list.Add("You"); | |
| DebugUtil.ListDump(data); | |
| DebugUtil.ListDump(list); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class DebugUtil | |
| { | |
| public static void ListDump<T>(params T[] datas) | |
| { | |
| ListDump((IEnumerable<T>)datas);//キャストすることで、もう一つのListDumpを呼び出し | |
| } | |
| public static void ListDump<T>(IEnumerable<T> datas) | |
| { | |
| foreach (var data in datas) | |
| { | |
| Console.WriteLine(data); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DebugUtil.ListDump("ほげ","ふが"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static void Main(string[] args) | |
| { | |
| var data = new[] {1, 2, 3, 4, 5}; | |
| var list = new List<string>(); | |
| list.Add("Hello"); | |
| list.Add("Nice"); | |
| list.Add("Too"); | |
| list.Add("Meet"); | |
| list.Add("You"); | |
| DebugUtil.ListDump(data);//OK | |
| DebugUtil.ListDump(list);//NG | |
| DebugUtil.ListDump("ほげ","ふが");//OK | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DebugUtil.ListDump((IEnumerable<string>)list); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class DebugUtil | |
| { | |
| public static void ListDump(params object[] datas) | |
| { | |
| ListDump((IEnumerable) datas); | |
| } | |
| public static void ListDump(IEnumerable datas) | |
| { | |
| foreach (var data in datas) | |
| { | |
| Console.WriteLine(data); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class DebugUtil | |
| { | |
| public static void ListDump(params object[] datas) | |
| { | |
| if (datas.Length == 0) return; | |
| if (datas.Length == 1) | |
| { | |
| var data = datas[0] as ICollection; | |
| if (data != null) | |
| { | |
| ListDump(data); | |
| return; | |
| } | |
| Console.WriteLine(datas[0]); | |
| return; | |
| } | |
| ListDump((IEnumerable) datas); | |
| } | |
| public static void ListDump(IEnumerable datas) | |
| { | |
| foreach (var data in datas) | |
| { | |
| ListDump(data); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment