473,383 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

params (rant)

Given a method like

void F ( params object[] args ) { ... }

I had always _assumed_ that args would never be null, but now it's been
brought to my attention that if the caller uses

F ( null )

then args _will_ be null because the compiler can implicitly convert the
null to an object[].

But I doubt that this is the desired behaviour in most cases, certainly _I_
expect (and prefer) args to be an array containing one null, just as

F ( null , null )

results in an array containing two nulls.

Some experimentation has shown that

F ( new object[] { null } )
and
F ( (object) null )

result in the desired array, but the caller shouldn't have to go through
such hoops.

It seems to me that rather than have the compiler see if the value can be
implicitly converted to the array type, it shoud determine if the value _is_
the array type.
I realize that it's too late to change the language, and if someone _does_
want the parameter to be null, then the current behaviour seems the only way.
But, man, now I have to go through my code looking for methods that take
params and protect against null.

Sep 28 '08 #1
9 1617
PIEBALD <PI*****@discussions.microsoft.comwrote:
Given a method like

void F ( params object[] args ) { ... }

I had always _assumed_ that args would never be null, but now it's been
brought to my attention that if the caller uses

F ( null )

then args _will_ be null because the compiler can implicitly convert the
null to an object[].
Indeed. params *allows* the caller to use the F(x, y, z) syntax, but it
doesn't *force* it.
But I doubt that this is the desired behaviour in most cases, certainly _I_
expect (and prefer) args to be an array containing one null, just as

F ( null , null )

results in an array containing two nulls.
Note that this will only be the behaviour when you're using the
*literal* null. If you do:

object o = null;
F(o);

or something similar, that will be handled as you expect. I think it's
relatively rare to need to call a method with params parameter passing
a null literal - and given that you might *also* want to call it
passing a null argument instead of an array containing a null value,
the language has to make a trade-off anyway.
It seems to me that rather than have the compiler see if the value can be
implicitly converted to the array type, it shoud determine if the value _is_
the array type.
I disagree. I think it's fine the way it is.
I realize that it's too late to change the language, and if someone _does_
want the parameter to be null, then the current behaviour seems the only way.
But, man, now I have to go through my code looking for methods that take
params and protect against null.
You should have done so anyway - or at least expected that the value
could be null. After all, you could have:

object[] x = null;
F(x);

Are you surprised that the above compiles? If not, what would you have
expected it to do?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 28 '08 #2
You should have done so anyway

So it seems.

Sep 28 '08 #3
On Sun, 28 Sep 2008 11:22:37 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
[...]
>But, man, now I have to go through my code looking for methods that take
params and protect against null.

You should have done so anyway - or at least expected that the value
could be null. After all, you could have:

object[] x = null;
F(x);

Are you surprised that the above compiles? If not, what would you have
expected it to do?
For what it's worth, I think part of the issue is that people coming from
C++ may be viewing the "params" argument declaration to work like C++'s
variable-length argument list, in that you never have direct access to the
list itself when using the standard parameter list syntax.

With that perspective, I think one could expect your example to compile to
a single-element parameter list with the value of "null" as that element.
In other words, the parameter array would still not be null; it would
_contain_ a null.

I realize that's just not how C# works, but it would have been a
reasonable design, at least ignoring other side-effects and complexities
it would introduce to C#.

Pete
Sep 28 '08 #4
On Sep 29, 12:04*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

<snip>
With that perspective, I think one could expect your example to compile to a single-element parameter list with the value of "null" as that element. *
In other words, the parameter array would still not be null; it would *
_contain_ a null.
Good point. Here's a clearer example, without the ambiguity stemming
from the fact that object[] can be converted to object:

void Foo(params string[] args)
{
}
....
string[] x = null;
Foo(x);

Clearly that can't be treating x as a string, because it's a string[].
Therefore if the above compiles, it's possible for a "params"
parameter to be null. Therefore one should check "params" parameters
for nullity in the same way as other parameters.

The only argument against that would be if the OP wouldn't have
expected the above code to compile - which would be a horrible
restriction, IMO.

Jon
Sep 29 '08 #5
On Sun, 28 Sep 2008 23:13:45 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
[...]
Good point. Here's a clearer example, without the ambiguity stemming
from the fact that object[] can be converted to object:

void Foo(params string[] args)
{
}
...
string[] x = null;
Foo(x);

Clearly that can't be treating x as a string, because it's a string[].
Therefore if the above compiles, it's possible for a "params"
parameter to be null. Therefore one should check "params" parameters
for nullity in the same way as other parameters.
Well, yes. I agree with the basic logic. But trying to apply it in a
practical sense is difficult. After all, to do so assumes that the OP
would have written a bit of test code like that, just to see what was
legal and what wasn't.

I admit, I find myself doing that sort of thing, and it wouldn't surprise
me if you do too. But it's hardly the sort of thing I expect every
developer to do as part of their normal development process.

I do it because I like the peek inside the box to see what's making things
tick (okay, it's more like toppling the stack of presents Mom tried to
hide on the shelf, and then shaking them vigorously...but whatever :) ).
But lots of programmers just want to get the job done, and don't have the
inclination and/or time to mess around with poking and prodding the
compiler to explore the corner cases.

In particular, in this case I think had it occurred to the OP to write
test code like the above, he probably would have just tested the "passing
null directly" case to see what happens.

I don't know what the OP's true feelings are on the question (that is, is
he actually mad at the language, or was he just frustrated for a moment),
but at the very least, I can see why he felt a need to blow off a little
steam with his rant. :)
The only argument against that would be if the OP wouldn't have
expected the above code to compile - which would be a horrible
restriction, IMO.
"Horrible"? I don't know. I mean, I do appreciate the C# implementation
of variable-length parameter lists. But other languages get by just
fine. If anything, because of the way C# does it, in reality it really is
just glossy syntax on top of a very basic idea: passing an array.

Had C# prohibited the example you offer here, I don't think it'd be that
big of a deal. You just couldn't use variable-length parameter lists in a
scenario where what you really wanted to pass was a reference to an array,
possibly null. You'd have to declare and use it more explicitly. No big
deal, at least for me it wouldn't be.

Pete
Sep 29 '08 #6
And once again I find myself yearning for the missing "argument cannot
be null" language feature. Maybe one day...

Marc
Sep 29 '08 #7
On Sep 28, 10:22*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
You should have done so anyway - or at least expected that the value
could be null. After all, you could have:

object[] x = null;
F(x);

Are you surprised that the above compiles? If not, what would you have
expected it to do?
I think the problem here is that when checking for null params, one
could reasonably think that just throwing ArgumentNullException would
be fine (because it handles the case above). But then it can puzzle
the client, who can do:

F(null);

and get an ArgumentNullException on that, but not on:

F(null, null);

which is certainly rather confusing.
Sep 29 '08 #8
On Sep 29, 7:35*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

<snip>
"Horrible"? *I don't know. *I mean, I do appreciate the C# implementation *
of variable-length parameter lists. *But other languages get by just *
fine. *If anything, because of the way C# does it, in reality it reallyis *
just glossy syntax on top of a very basic idea: passing an array.

Had C# prohibited the example you offer here, I don't think it'd be that *
big of a deal. *You just couldn't use variable-length parameter lists in a *
scenario where what you really wanted to pass was a reference to an array, *
possibly null. *You'd have to declare and use it more explicitly. *Nobig *
deal, at least for me it wouldn't be.
I think it would have been a significant limitation. For instance,
consider this exception constructor:

public MyException (string format, params object[] args) :
base(string.Format(format, args))
{
...
}

I use that kind of thing reasonably often - I like to be able to make
it easy for callers to format text without explicitly calling
string.Format themselves. However, the restriction above would prevent
*my* code from calling string.Format. We'd potentially have to have
overloads for everything - string.Format(string, object[]),
string.Format(string, params object[]) which just ends up being silly
IMO. It would also rely on everyone who implemented a method with a
"params" parameter being considerate enough to provide the overload.

Jon
Sep 29 '08 #9
On Mon, 29 Sep 2008 00:23:58 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
On Sep 29, 7:35Â*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

<snip>
>"Horrible"? Â*I don't know. Â*[...]

I think it would have been a significant limitation.
I'm going to go with "eye of the beholder" here.
For instance,
consider this exception constructor:

public MyException (string format, params object[] args) :
base(string.Format(format, args))
{
...
}

I use that kind of thing reasonably often - I like to be able to make
it easy for callers to format text without explicitly calling
string.Format themselves. However, the restriction above would prevent
*my* code from calling string.Format. [...]
Not necessarily. C# could just provide a syntax that allows for a
different mechanism to forward variable-length parameter lists. After
all, that's what C++ does.

But in any case, while I don't disagree with your example, I do disagree
with the claim of seriousness. It's inconvenient, to be sure. But
"horrible"? I remain unconvinced that it rises to that level of badness.
:)

Pete
Sep 29 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Bart Nessux | last post by:
How can one view the contents of a queue? I'd like to verify that the queue has the same number of objects as the list that has been put into it. Also, should one think of a queue as static or...
0
by: Trevor Best | last post by:
<rant> Spent an hour yesterday figuring that one out in my sp, on an insert statement that used a join for the source. Even rebooted the server in desperation. It wasn't until I took that join and...
0
by: Mihaly | last post by:
Hello, i'm using a VC++ com+ object, it's reference in vs .net, this com object have a function that have to params, the interop com object recive Object Params and the original com object say...
2
by: Tobias Olbort | last post by:
Hello, i've a outer function, which takes a params-array as a parameter. I want to pass this array to inner function with a params-array (e. g. string.format). When i've passed an integer to...
0
by: Dominik Tropper | last post by:
Dear all I have created a c# web project in VS.NET 2003 that contains a crystal report with two params. No problems until here, but executing the report selects the wrong records. My params...
3
by: Stan Huff | last post by:
Is there any way to disable the "params" on a particular invocation so that one can pass an array containing the arguments and not have receiver get an array having you argument array stuffed into...
8
by: David Duerrenmatt | last post by:
Hi there For some reasons, I've to use Python 1.5.2 and am looking for a workaround: In newer Python versions, I can call a function this way: func = some_function func(*params) Then,...
8
by: Jamie | last post by:
Hello Newsgroup: This is my little rant about security and why we have home directories. You may choose to ignore it or disagree with it, that is your perogative and I won't care, but... this...
2
by: FFMG | last post by:
Hi, I was looking at http://www.php.net/manual/en/function.call-user-func-array.php and I was wondering... Given, // -- function foo( &$params) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.