C# Arrays vs. List — Why You Need .ToList()
The problem
A field is declared as an array, but the method it’s passed to expects a list.
// Field declaration: array
private string[] OutProcessCd = { "PR002", "PR003", "PR006", "PR007", "PR008" };
// Method parameter: list
public DataTable Select_Process(List<string> processCds = null, bool all_Yn = false)
Passing OutProcessCd directly is a compile error — the types don’t match.
Why they’re different types
| Type | Example |
|---|---|
string[] (array) |
{ "PR002", "PR003" } — fixed size |
List<string> (list) |
new List<string> { "PR002", "PR003" } — resizable |
An array’s size is fixed at declaration; a list can grow and shrink. Because they’re distinct types, there’s no implicit conversion between them.
The fix: .ToList()
LINQ’s .ToList() converts an array into a list.
string[] arr = { "A", "B", "C" };
List<string> list = arr.ToList(); // array → list
So the call site just becomes Select_Process(OutProcessCd.ToList()) — that’s it.
One-line summary
Arrays and lists are different types. .ToList() is just the conversion that bridges them,
not some deeper piece of logic.