how to pass in empty value for array argument?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tee

    how to pass in empty value for array argument?

    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.Empt y)

    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


  • Jon Skeet [C# MVP]

    #2
    Re: how to pass in empty value for array argument?

    Tee <thy@streamyx.c om> wrote:[color=blue]
    > 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.Empt y)[/color]

    No, both of those *are* passing strings. Empty strings, but strings
    nonetheless.
    [color=blue]
    > 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?[/color]

    What I think you're after is null.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Chris Ballard

      #3
      Re: how to pass in empty value for array argument?

      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:
      [color=blue]
      > Tee <thy@streamyx.c om> wrote:[color=green]
      > > 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.Empt y)[/color]
      >
      > No, both of those *are* passing strings. Empty strings, but strings
      > nonetheless.
      >[color=green]
      > > 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?[/color]
      >
      > What I think you're after is null.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too
      >[/color]

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: how to pass in empty value for array argument?

        Chris Ballard <worldoflard@ty peYAHOOhere.com > wrote:[color=blue]
        > I believe the current best practise is to pass in an empty array, such as:
        > ... xyz( new string[0] );[/color]

        That entirely depends on what the method is expecting. Some methods
        will accept null and others won't.
        [color=blue]
        > 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.[/color]

        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.
        [color=blue]
        > 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.[/color]

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

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Chris Ballard

          #5
          Re: how to pass in empty value for array argument?

          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:
          [color=blue]
          > Chris Ballard <worldoflard@ty peYAHOOhere.com > wrote:[color=green]
          > > I believe the current best practise is to pass in an empty array, such as:
          > > ... xyz( new string[0] );[/color]
          >
          > That entirely depends on what the method is expecting. Some methods
          > will accept null and others won't.
          >[color=green]
          > > 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.[/color]
          >
          > 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.
          >[color=green]
          > > 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.[/color]
          >
          > Not necessarily - it's up to the method. Many of the public methods I
          > write will throw a NullArgumentExc eption if null is passed when it
          > shouldn't be.
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too
          >[/color]

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: how to pass in empty value for array argument?

            Chris Ballard <worldoflard@ty peYAHOOhere.com > wrote:[color=blue]
            > 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.[/color]

            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 - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            Working...