I have the following method
private static void WriteReport<T>(List<T> report, string reportName)
{
using (StreamWriter sw = new StreamWriter(@path)
{
for (var row = 0; row < report.Count; row++)
{
for (var column = 0; column < report.ElementAt(row).Count; column++)
sw.Write(report[row][column] + ",");
sw.WriteLine();
}
}
}
I can only pass in the above method the List<object>
as parameter but I also want to pass parameter List<List<object>>
in some cases. But I couldn't figure out the correct way to do so.
Edit: Cause whenever I pass List<List<object>>
only then the nested for loop can be applied but if I pass list<object>
then no nested loop can be applied and not sure how to structure my method to code it correctly
I wanted to know how to write a generic method to do so and also a bit curious to see if the generic method improves code maintainability and readability as well
You are not be able to convert T in List from T again, the solution that i see is to overload the method like this:
public static void Main()
{
WriteReport(new List<List<string>>()
{
new List<string>()
{
"Some Item",
"Onather Item"
},
new List<string>()
{
"Test Item",
"Demo Item"
}
}, "Report Name");
}
private static void WriteReport<T>(List<T> report, string reportName)
{
foreach (var item in report)
{
Console.WriteLine(item);
}
}
private static void WriteReport<T>(List<List<T>> report, string reportName)
{
foreach (var item in report)
{
WriteReport(item, reportName);
}
}
private static void WriteReport<T>(List<List<List<T>>> report, string reportName)
{
foreach (var item in report)
{
WriteReport(item, reportName);
}
}
private static void WriteReport<T>(List<List<List<List<T>>>> report, string reportName)
{
foreach (var item in report)
{
WriteReport(item, reportName);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.