开发者

object vs object[]

I am looking at a method that takes a parameter of object[]. But if I pass in a single value (ie an object) it still works.

Is this built in as part of C#? Are object and object[] the 开发者_运维百科same?

I ask, because sometimes I need to pass in an array of objects and sometimes I need just one and I am wondering if I need to do this:

public static void MergeRow(object primaryKey, object[] rowValues)
{
    MergeRow(new object[]{primaryKey}, rowValues);
}

public static void MergeRow(object[] primaryKey, object[] rowValues)
{
     // Rest of my method

or is the first signature enough and I can pass in an array or a single object as I like?

Basically I am asking do I need overload this method?

Can I just do this:

public static void MergeRow(object primaryKey, object[] rowValues)
{
    // Rest of my method

and pass in a object or object[].

(Note, the param I am talking about is the first one (primaryKey).

(Note: the method I am calling with the primaryKey parameter is SqlCeResultSet.Seek which also takes an object[])


You can accept a parameter array in certain situations, which allows you to provide 1 to n objects or a single array. Something like

public static void MyMethod(params object[] values) { }

The catch is that the params array must be the final parameter of the method. If you need the option of having a single object or an array of objects before the final parameter, you must provide an overload like your original code. Your code looks like you are trying to work with the first parameter, so your answer is that overloads are your friend, as you cannot pass in an object where an object array is expected without using the params keyword, and that is not an option for anything but the last parameter.

Edit Based on the additional information, SqlCeDataReader.Seek does take a params array as its final parameter, so yes... you can call it with a single object, multiple objects, or an array of objects.

These calls are all legal

Seek(DbSeekOptions.After, someObject);
Seek(DbSeekOptions.After, someObject, anotherObject);
Seek(DbSeekOptions.After, objectArray);
Seek(DbSeekOptions.After);


object and object[] aren't the same, and you won't be able to generally interchange them. There's nothing in C# that is transmuting them behind the scenes - you're probably running into parameter arrays where you've seen this before.

These are a special case where a method call will accept a list of individual objects of variable length as the last "parameter" of the method, allowing the caller to specify any number of additional parameters to the method call.

These are likely the cases where you're passing one object where the function definition says it needs an array of them.

Since your functions are taking multiple arrays and passing them through, if you want to eliminate the end user having to encapsulate their single object in an array, then you would need to provide both.

Honestly though, as an API consumer, I wouldn't be offended if you left the single-object overload out ;)


Yes, you need to overload the method, because passing a single object to the method accepting an array of objects will yield an error when compiling.

edit

In case you want it the other way round, it actually works and you can do it like this:

public static void MergeRow(object primaryKey, object[] rowValues)
{
    if ( primaryKey is object[] )
    {
        // is an array of objects
    }
    else
    {
        // is a single object
    }
}


If you just use the first signature, taking an object, you can pass in an object[] and it will compile fine.

However:

  • You'll have to handle this inside your method - check if what you've been given is an object[] and treat it differently from if it's just an object.
  • Someone seeing a method with an object parameter probably won't expect to be able to pass an object[].
  • This won't work for other classes - an object[] is an object but a something[] is not a something.


As others have stated, and following the post by womp, I would like to point out that object and object[] are not interchangeable and you're experiencing exactly what has been described. The SqlCeDataReader.Seek function is defined as:

public bool Seek (
    DbSeekOptions dbSeekOptions,
    params Object[] index
)

You can find more information about parameter arrays here:

http://msdn.microsoft.com/en-us/library/w5zay9db(VS.71).aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜