Question

The SqlDataReader class offers a method called GetValues. What I find curious about this method is that it takes an object[] as input and returns an int of the number of values copied into the array.

Any idea why the method was designed that way, rather than returning a new Array?

Was it helpful?

Solution

According to the documentation:

You can pass an Object array that contains fewer than the number of columns contained in the resulting row. Only the amount of data the Object array holds is copied to the array. You can also pass an Object array whose length is more than the number of columns contained in the resulting row.

This allows the caller to be in complete control of the memory allocation and also control the number of columns requested easily.

OTHER TIPS

Just a guess, but by passing in an array, you can reuse the same array over and over. Creating an array takes time. Not a lot of time, but if you are iterating over millions of results it can be a measurable performance gain by reusing the same array. Creating a new array each time will also cause the GC more work then necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top