4

I'm writing a simple C# class that handle SQL queries in a specific contest. Most of the queries will be SELECT statement with 3-4 parameters that should be escaped properly, so the function will be something like this

public DataTable Select(string query, string[] parameters) { # some code } 

Should parameters be an Array or a List<>? Is there some best practice when choosing between these two types as function parameter or is it just a matter of preferences?

P.S. I usually prefer List<> because of the flexibility but here the possibility to create an Array on the fly is a good point.

4
  • If there is any manipulation of the parameters I would go with a List as resizing arrays is expensive. If no resizing is necessary i would stick with an array. Commented Apr 27, 2015 at 12:41
  • Instead of "escaping" parameters use sql-parameters. Commented Apr 27, 2015 at 12:43
  • my advice : dont write this kind of generic data access stuff. its a waste of time. etiher make a db specifc repository which returns your objects or use the system.data generic data stuff Commented Apr 27, 2015 at 13:04
  • Refer to stackoverflow.com/a/2975522/3393095 Commented Apr 27, 2015 at 13:14

6 Answers 6

16

You should use IEnumerable, then it can be either since both list and array implement this

public DataTable Select(string query, IEnumerable<string> parameters) 

They also both implement IList and ICollection, which may offer other useful properties as shown by Tim Schmelter in the comments

Sign up to request clarification or add additional context in comments.

5 Comments

IEnumerable<string> you mean
Hmm, possibly it depends on what the OP wants to restrict to, Possibly IEnumerable<T> too, Thanks
If he wants to include only arrays or lists as mentioned he should use IList<string>. Then you can still access them by index without casting and use the Count property(yes, array has).
@TimSchmelter - Thanks, I've updated my answer to reflect your comment.
Warning: Using IEnumerable<T> can result in unexpected behaviour. See: jetbrains.com/help/resharper/PossibleMultipleEnumeration.html
9

According to the Robustness principle, or Postel's law (emphasis mine):

Be conservative in what you do, be liberal in what you accept from others

In other words, use the "broadest" type possible, the one higher up in the inheritance hierarchy, so as to:

  1. demand as little as possible from the user,
  2. and give the user as many choices as possible.

In this case:

  • if all you need to do is iterate through the collection of parameters, then you should demand from the user an instance of a type that can be iterated: which, in this case, is IEnumerable<T>.
  • If you needed to add/remove items, then ICollection<T> would be the most liberal option
  • If, for some reason, you need to access items by index, then you should demand a IList<T>.

Comments

2

On 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 that if you have a "static" query, you can use the first overload (because you know at compile time the number of parameters), while if you have a "dynamic" query you can use the second overload with a string[] or a List<string> or the result of a LINQ expression.

Comments

1

Personally, I would expect at least one overload that grants me the ability to pass a params array:

public DataTable Select(string query, params string[] parameters); 

That would allow me to call it with parameters like this:

Select("SELECT FROM WHERE", "3", "17", "Joe"); 

Anyone having an IEnumerable<> could pass it in easily, too:

Select("SELECT FROM WHERE", myData.ToArray()); 

Better would be an overload doing this for me.

Comments

0

In general I would use IEnumerable<T> if you only read from the sequence.

In this particular context however I'd simply use a params string[] or even params object[] type since then you can use it like String.Format, i.e.

var dt = Select("select A from Table where X=? and Y=?", value1, value2); 

Comments

0

If your parameters have all the same type (here string) you can use params keyword with string array.

public DataTable Select(string query, params string[] parameters) 

Thus you can call it like below

Select(myQuery, param1, param2, ...); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.