By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,795 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,795 IT Pros & Developers. It's quick & easy.

Why Are "fn(object[] parameters)" And "fn(params object[] parameters)" Ambiguous?

P: n/a
Hi,

Why are

fn(object[] parameters)

and

fn(params object[] parameters)

ambiguous? Or at least the compiler doesn't like both. The
calls appear to be unambiguous to me

object [] x = ...;
fn(x);

and

fn(x1, x2, x3, ...);

It is often convenient to have both with the second form
just calling the first.

In looking at the examples here I think I see the problem
but will post just to see if I'm right. The first example is
ambiguous because "object[]" is itself an object. Right?

Thanks,
Gary
Jun 28 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Gary Brown wrote:
Hi,

Why are

fn(object[] parameters)

and

fn(params object[] parameters)

ambiguous? Or at least the compiler doesn't like both. The
calls appear to be unambiguous to me

object [] x = ...;
fn(x);

and

fn(x1, x2, x3, ...);

It is often convenient to have both with the second form
just calling the first.

In looking at the examples here I think I see the problem
but will post just to see if I'm right. The first example is
ambiguous because "object[]" is itself an object. Right?


Not quite. They're ambiguoius because the two overloads in fact have the
exact same signature - fn(object[]). The params keyword simply decorates
that parameter with an attribute (System.ParamsArrayAttribute) which clues
the compiler into fabricating an object[] out of whatever you happen to pass
to the function.

-cd
Jun 28 '06 #2

P: n/a
Gary Brown wrote:
Hi,

Why are

fn(object[] parameters)

and

fn(params object[] parameters)

ambiguous? Or at least the compiler doesn't like both. The
calls appear to be unambiguous to me

object [] x = ...;
fn(x);

and

fn(x1, x2, x3, ...);

It is often convenient to have both with the second form
just calling the first.

In looking at the examples here I think I see the problem
but will post just to see if I'm right. The first example is
ambiguous because "object[]" is itself an object. Right?

Thanks,
Gary


Hi Gary,

The params keyword isn't a built-in .NET feature, it's sugar for applying an
attribute to the parameter, specifically ParamsArrayAttribute. It lets
other compilers know that the parameters after it can all be condensed into
one handy object array.

--
Hope this helps,
Tom Spink
Jun 28 '06 #3

P: n/a

Gary Brown wrote:
Hi,

Why are

fn(object[] parameters)

and

fn(params object[] parameters)

ambiguous? Or at least the compiler doesn't like both. The
calls appear to be unambiguous to me

object [] x = ...;
fn(x);

and

fn(x1, x2, x3, ...);

It is often convenient to have both with the second form
just calling the first.


Because the first form isn't necessary. If you declare a method

void fn (params object[] parameters)

then you can also pass it an array of object.

Jun 28 '06 #4

P: n/a
>> Why are
fn(object[] parameters)
and
fn(params object[] parameters)
ambiguous? Or at least the compiler doesn't like both. The
Because the first form isn't necessary. If you declare a method


Thanks, that really helps. I never thought of that interpretation.
This makes the form more useful.

Gary
Jun 28 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.