I have a method to perform operation in Datatable.
public DataTable SetColumnsOrder(DataTable table, String columnNames)
int columnIndex = 0;
foreach (var columnName in columnNames)
} return table;
To access this method I need to do like this
dt = SetColumnsOrder(dt,colNames);
Instead of doing like this, how to create a function to call it like below in c#
where the function should take dt as input to perform operations and store back in same dt.
First of all, you don’t need to return the same DataTable that you pass in. You could change your method signature to:public void SetColumnsOrder(DataTable table, String columnNames)
and remove the return, and it would still work the same (obviously you’d call it like SetColumnsOrder(dt,colNames); instead of dt = SetColumnsOrder(dt,colNames);. And you should do that, because it’s less confusing design.
Then, in order to call it as an extension method, just change the signature again, to:public static void SetColumnsOrder(this DataTable table, String columnNames)
And now you can use it like dt.SetColumnOrder(colNames);.