C # function parameter Array vs List
I am writing a simple C # class that handles SQL queries in a specific competition. Most of the requests will be SELECT
with parameters 3-4, which must be escaped properly, so the function will look something like this:
public DataTable Select(string query, string[] parameters) {
# some code
}
Should there parameters
be Array
or List<>
? Is there some kind of best practice when choosing between these two types as a function parameter, or is it just a matter of preference?
PS I usually prefer it List<>
because of the flexibility, but here's a good opportunity to create Array
on the fly.
source to share
You have to use IEnumerable, then it can be either with list
, and array
implement this
public DataTable Select(string query, IEnumerable<string> parameters)
They also implement IList
and ICollection
, which may offer other useful properties, as demonstrated by Tim Schmelter in the comments
source to share
According to the principle of "Trustworthiness" or "Postel Law" (emphasis mine):
Be conservative in what you do, be liberal in what you accept from others.
In other words, use the "widest" type that is higher than the inheritance hierarchy to:
- demand as little as possible from the user,
- and provide the user with as many options as possible.
In this case:
- If all you have to do is iterate over a set of parameters, then you must require the user to have an instance of a type that can be iterated over: in this case, it is
IEnumerable<T>
. - If you needed to add / remove elements, that
ICollection<T>
would be the most liberal option - If for some reason you need to access the elements by index, then you should require
IList<T>
.
source to share
At the top of what nvoig said, I would:
public DataTable Select(string query, params string[] parameters)
{
return Select(query, (IEnumerable<string>)parameters);
}
public DataTable Select(string query, IEnumerable<string> parameters)
{
return null;
}
so if you have a "static" query you can use the first overload (because you specified the number of parameters at compile time), and if you have a "dynamic" query you can use the second overload with string[]
or List<string>
or the result of a LINQ expression ...
source to share
Personally, I would have expected at least one overload that gives me the ability to pass an array of params:
public DataTable Select(string query, params string[] parameters);
This would allow me to call it with parameters like this:
Select("SELECT FROM WHERE", "3", "17", "Joe");
Anyone with IEnumerable<>
can easily pass it:
Select("SELECT FROM WHERE", myData.ToArray());
It would be better to overload doing this for me.
source to share
In general, I would use IEnumerable<T>
if you were only reading from sequence.
In this particular context, however, I would just use a type params string[]
or even params object[]
, since you can use it like String.Format
, i.e.
var dt = Select("select A from Table where X=? and Y=?", value1, value2);
source to share