469,889 Members | 1,352 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,889 developers. It's quick & easy.

how to pass in empty value for array argument?

Tee
Hi,

When I have a function like:
private string xyz(string Text)

I can call it without passing in a string with xyz("") or xyz(string.Empty)

but if the paramater needed is an array like:
private string xyz(string[] ArrayName)

how do I call it without giving an array using the same idea as above?
Thanks,
Tee
Nov 16 '05 #1
5 21272
Tee <th*@streamyx.com> wrote:
When I have a function like:
private string xyz(string Text)

I can call it without passing in a string with xyz("") or xyz(string.Empty)
No, both of those *are* passing strings. Empty strings, but strings
nonetheless.
but if the paramater needed is an array like:
private string xyz(string[] ArrayName)

how do I call it without giving an array using the same idea as above?


What I think you're after is null.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Jon,

I believe the current best practise is to pass in an empty array, such as:
.... xyz( new string[0] );

Especially if this is an internally called routine where you trust the input
parameters, as you can go straight into (for example) a foreach loop without
the additional branches required to test for null.

Obviously if this may be called by third party code then you will need to
check this for null first, and therefore either technique would be suitable.

Cheers,
Chris.
"Jon Skeet [C# MVP]" wrote:
Tee <th*@streamyx.com> wrote:
When I have a function like:
private string xyz(string Text)

I can call it without passing in a string with xyz("") or xyz(string.Empty)


No, both of those *are* passing strings. Empty strings, but strings
nonetheless.
but if the paramater needed is an array like:
private string xyz(string[] ArrayName)

how do I call it without giving an array using the same idea as above?


What I think you're after is null.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Chris Ballard <wo*********@typeYAHOOhere.com> wrote:
I believe the current best practise is to pass in an empty array, such as:
... xyz( new string[0] );
That entirely depends on what the method is expecting. Some methods
will accept null and others won't.
Especially if this is an internally called routine where you trust the input
parameters, as you can go straight into (for example) a foreach loop without
the additional branches required to test for null.
Possibly - or possibly passing in something which isn't an array at all
(which is what was stated as the goal) may have distinct meaning.
Obviously if this may be called by third party code then you will need to
check this for null first, and therefore either technique would be suitable.


Not necessarily - it's up to the method. Many of the public methods I
write will throw a NullArgumentException if null is passed when it
shouldn't be.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Jon,

Sorry I was making the assumption that xyz is an internally created method,
and my response was from the standpoint of the implementor of such a method.

Obviously if this method is part of a third party library, then anyone
calling into it will need to consult the related documentation to find out
what values are appropriate for indicating an "empty" parameter.

Chris.

"Jon Skeet [C# MVP]" wrote:
Chris Ballard <wo*********@typeYAHOOhere.com> wrote:
I believe the current best practise is to pass in an empty array, such as:
... xyz( new string[0] );


That entirely depends on what the method is expecting. Some methods
will accept null and others won't.
Especially if this is an internally called routine where you trust the input
parameters, as you can go straight into (for example) a foreach loop without
the additional branches required to test for null.


Possibly - or possibly passing in something which isn't an array at all
(which is what was stated as the goal) may have distinct meaning.
Obviously if this may be called by third party code then you will need to
check this for null first, and therefore either technique would be suitable.


Not necessarily - it's up to the method. Many of the public methods I
write will throw a NullArgumentException if null is passed when it
shouldn't be.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
Chris Ballard <wo*********@typeYAHOOhere.com> wrote:
Sorry I was making the assumption that xyz is an internally created method,
and my response was from the standpoint of the implementor of such a method.

Obviously if this method is part of a third party library, then anyone
calling into it will need to consult the related documentation to find out
what values are appropriate for indicating an "empty" parameter.


I don't think the third-party/internal divide makes any difference, to
be honest. In some cases it's a good idea to use an empty array for
parameters, in some cases null is more appropriate. It depends on
whether you want to pass an empty collection of values or say "I have
no collection to pass, not even an empty one". That decision needs to
be made both for internal methods and public ones.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by wilson | last post: by
14 posts views Thread by Abhi | last post: by
24 posts views Thread by =?Utf-8?B?U3dhcHB5?= | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.