回答編集履歴
2
追記
answer
CHANGED
@@ -1,7 +1,32 @@
|
|
1
1
|
回答させていただきます。
|
2
2
|
引数は配列にすべきではないですか?
|
3
3
|
|
4
|
+
### [追記] Zuishinさんのアドバイスを元に回答を修正します。
|
5
|
+
[List<T>](https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.generic.list-1?view=netframework-4.7.2)や[配列](https://docs.microsoft.com/ja-jp/dotnet/csharp/programming-guide/arrays/)はどちらも[IEnumerable<T>](https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.generic.ienumerable-1?view=netframework-4.7.2)を実装しているので、オーバーロードするのではなく下記のように変更しました。[Zuishin](https://teratail.com/users/Zuishin)さんが詳しく説明されておりますので是非コメントもお読みください。
|
6
|
+
|
7
|
+
[Zuishin](https://teratail.com/users/Zuishin)さんありがとうございます。
|
8
|
+
大変勉強になりました。
|
9
|
+
|
4
10
|
```C#
|
11
|
+
string ListSum<Type>(IEnumerable<Type> obj)
|
12
|
+
{
|
13
|
+
string str = "";
|
14
|
+
foreach (Type st in obj)
|
15
|
+
{
|
16
|
+
str += $"{st,-10}";
|
17
|
+
}
|
18
|
+
return str;
|
19
|
+
}
|
20
|
+
```
|
21
|
+
|
22
|
+
**IEnumerable<T> Interface**
|
23
|
+
指定した型のコレクションに対する単純な反復処理をサポートする列挙子を公開します。
|
24
|
+
|
25
|
+
参照元 [https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.generic.ienumerable-1?view=netframework-4.7.2](https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.generic.ienumerable-1?view=netframework-4.7.2)
|
26
|
+
|
27
|
+
### 旧回答
|
28
|
+
|
29
|
+
```C#
|
5
30
|
string ListSum<Type>(Type[] obj)
|
6
31
|
{
|
7
32
|
string str = "";
|
1
追記
answer
CHANGED
@@ -11,4 +11,14 @@
|
|
11
11
|
}
|
12
12
|
return str;
|
13
13
|
}
|
14
|
+
|
15
|
+
string ListSum<Type>(List<Type> obj)
|
16
|
+
{
|
17
|
+
string str = "";
|
18
|
+
foreach (Type st in obj)
|
19
|
+
{
|
20
|
+
str += $"{st,-10}";
|
21
|
+
}
|
22
|
+
return str;
|
23
|
+
}
|
14
24
|
```
|