Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Newbie question: single/multiple method arguments

Newbie question: single/multiple method arguments

Scheduled Pinned Locked Moved C#
questionhelpcsharpdata-structurestutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Metal76
    wrote on last edited by
    #1

    Stupid question, I admit, but I'm quite new to C#: how can I write a method which accepts a single input parameter or a collection (e.g. array) of this parameter? For example, let's say I have the method void MyMethod(int[] args). If I try to call the function with a single argument, like MyMethod(1234), the compiler obviously throws an error (int != int[]). Overloading surely solves the issue (i.e. defining both void MyMethod(int[] args) and void MyMethod(int arg) and factoring out the common behaviour in a different private method) but is there a simpler way to do this? Regards, Andrea

    M 1 Reply Last reply
    0
    • M Metal76

      Stupid question, I admit, but I'm quite new to C#: how can I write a method which accepts a single input parameter or a collection (e.g. array) of this parameter? For example, let's say I have the method void MyMethod(int[] args). If I try to call the function with a single argument, like MyMethod(1234), the compiler obviously throws an error (int != int[]). Overloading surely solves the issue (i.e. defining both void MyMethod(int[] args) and void MyMethod(int arg) and factoring out the common behaviour in a different private method) but is there a simpler way to do this? Regards, Andrea

      M Offline
      M Offline
      Metal76
      wrote on last edited by
      #2

      I think I've just found the answer by myself: void MyMethod(params int[]). It was a stupid question, indeed! Bye, Andrea

      S P G 3 Replies Last reply
      0
      • M Metal76

        I think I've just found the answer by myself: void MyMethod(params int[]). It was a stupid question, indeed! Bye, Andrea

        S Offline
        S Offline
        subai
        wrote on last edited by
        #3

        that was not a stupid question, it was just simple, sometimes you want to find something that you can describe it to a person but can not describe it to a search engine

        I Wish the Life Had CTRL-Z Wizard's First Rule : People are fool,they believe what they want to believe or what they afraid to believe www.subaitech.blogspot.com

        1 Reply Last reply
        0
        • M Metal76

          I think I've just found the answer by myself: void MyMethod(params int[]). It was a stupid question, indeed! Bye, Andrea

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Yes, that's how I'd go, but you could also overload the method, have both methods. Oh, of course, you said that. What I should add, is that with overloading, you don't need to "factor out to a private method", you could simply have one method call the other:

          void MyMethod(int[] args) { ... }

          void MyMethod(int arg) { MyMethod ( new int[] { arg } ) ; }

          And leave all the functionality in the one main method. I rarely have to resort to a private method.

          1 Reply Last reply
          0
          • M Metal76

            I think I've just found the answer by myself: void MyMethod(params int[]). It was a stupid question, indeed! Bye, Andrea

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            Metal76 wrote:

            It was a stupid question, indeed!

            Not at all, the solution is not very obvious. It might not be the entire solution, either. You may want to overload that method with one that takes a single int, that way you can call it without having the compiler generate code that creates an array object every time (for the params array). Perhaps also add overloads that takes two and three parameters. The string.Concat method is overloaded in that way, for example. Another overload that you may want to add is one that takes an IEnumerable<int>, that will work for an int[], but also for other collections, like a List<int>.

            Despite everything, the person most likely to be fooling you next is yourself.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups