how to pass in empty value for array argument?

  • Thread starter Thread starter Tee
  • Start date Start date
T

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
 
Tee said:
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,

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 said:
Tee said:
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.
 
Chris Ballard said:
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,

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 said:
Chris Ballard said:
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.
 
Chris Ballard said:
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.
 
Back
Top