I created a class to operate with concrete table and I want to generalize this code to develop more universal class.

Here is one method of this class:

public void FillColumns(DataTable table, OracleDataReader reader)
{
{
table.Rows.Clear();
(
);
}
}


How can I refactor this concrete method, to make concrete column names as input parameters (even independing of number of columns)?

• Yes, I'm talking about only one method which I want to refactor. – Gordon Freeman Apr 24 '17 at 10:36
• Does it has to be a DataTable? Comparing with POCOs, DataTable is a bit slower. – Kosala Nuwan Perera Apr 24 '17 at 11:47

# params (version 1)

You can use the same trick the Rows.Add method uses namely the params keyword

By using the params keyword, you can specify a method parameter that takes a variable number of arguments.

and pass columns names as the last parameters. Then you read the values form the reader with some LINQ and turn it into an array because it's just that, an array (but one that is created dynamically if each value is passed separately). Besides you don't need two ifs, use the and && operator instead.

public void FillColumns(
DataTable table,
params string[] columnNames)
{
{
table.Rows.Clear();
{
}
}
}


Example call:

.FillColumns(table, reader, "Column1", "Column2");


# table.Columns (version 2)

the second approach would be to read the column names from the data table

public void FillColumns(DataTable table, OracleDataReader reader)
{
{
table.Rows.Clear();
{
table
.Columns
.Cast<DataColumn>()
.ToArray());
}
}
}


public void FillColumns(DataTable table, OracleDataReader reader)
{