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. The Lounge
  3. C# Optional Parameters?

C# Optional Parameters?

Scheduled Pinned Locked Moved The Lounge
csharpcomtestingtools
27 Posts 13 Posters 20 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.
  • R RCoate

    I just read this[^] and thought "what on earth is going on here"? When I moved from VB.Net to C#.net, one of the big evangelistic arguments was around optional parameters as opposed to overloaded methods. I always liked optional parameters, but was prepared to give them up if the general feeling was that they where evil. Seems they aren't evil any more. I think Microsoft is just messing with my head and they will be removed in 5.0. I do like the named parameters though. I have been wanting those ever since I did some Office Automation stuff.

    S Offline
    S Offline
    Stuart Dootson
    wrote on last edited by
    #21

    RCoate wrote:

    I do like the named parameters though. I have been wanting those ever since I did some Office Automation stuff.

    I've loved named parameters since I first did some Ada programming in the mid 1990s...thy'r the one thing that makes optional parameters non-evil, IMO...

    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

    1 Reply Last reply
    0
    • E Electron Shepherd

      Named parameters have the same problem. How do you guarantee that you have passed in the fourth parameter everywhere you should have?

      Server and Network Monitoring

      D Offline
      D Offline
      Dan Neely
      wrote on last edited by
      #22

      Electron Shepherd wrote:

      How do you guarantee that you have passed in the fourth parameter everywhere you should have?

      How do you know you called the 4 parameter overload everywhere you should have instead of the three parameter overload?

      3x12=36 2x12=24 1x12=12 0x12=18

      E 1 Reply Last reply
      0
      • D Dan Neely

        Electron Shepherd wrote:

        How do you guarantee that you have passed in the fourth parameter everywhere you should have?

        How do you know you called the 4 parameter overload everywhere you should have instead of the three parameter overload?

        3x12=36 2x12=24 1x12=12 0x12=18

        E Offline
        E Offline
        Electron Shepherd
        wrote on last edited by
        #23

        Becuase my point was to make the parameters non-optional. Then the compiler spots all those cases for you.

        Server and Network Monitoring

        1 Reply Last reply
        0
        • E Electron Shepherd

          The trouble with optional parameters comes when you extend them

          public static void DisplayName (string lastName, string firstName,
          string middleName = null)

          If I now add a salutation:

          public static void DisplayName (string lastName, string firstName,
          string middleName = null, string salutation = null)

          all my code still compiles. And that can be a problem. I have a lot of work to do to identify all the places that I need to pass in the new, fourth, parameter (I must need it in at least one place, or else why change the function). Without optional parameters, the compiler does my impact analysis for me. In this case, the worst that happens is that the salutation is missed off a displayed name, but in some cases, you can introduce some subtle bugs.

          Server and Network Monitoring

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

          Electron Shepherd wrote:

          I must need it in at least one place

          Not if it's framework code that you may not be using at all. I write a lot of methods I never use.

          1 Reply Last reply
          0
          • E Electron Shepherd

            The trouble with optional parameters comes when you extend them

            public static void DisplayName (string lastName, string firstName,
            string middleName = null)

            If I now add a salutation:

            public static void DisplayName (string lastName, string firstName,
            string middleName = null, string salutation = null)

            all my code still compiles. And that can be a problem. I have a lot of work to do to identify all the places that I need to pass in the new, fourth, parameter (I must need it in at least one place, or else why change the function). Without optional parameters, the compiler does my impact analysis for me. In this case, the worst that happens is that the salutation is missed off a displayed name, but in some cases, you can introduce some subtle bugs.

            Server and Network Monitoring

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #25

            You would have run into the same problem if you'd used method overloading instead of optional parameters; you'd still have to manually identify the places where the extra parameter should be used. Anyway, you could just comment out the = null and recompile again :)

            Regards Senthil _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

            1 Reply Last reply
            0
            • R RCoate

              I just read this[^] and thought "what on earth is going on here"? When I moved from VB.Net to C#.net, one of the big evangelistic arguments was around optional parameters as opposed to overloaded methods. I always liked optional parameters, but was prepared to give them up if the general feeling was that they where evil. Seems they aren't evil any more. I think Microsoft is just messing with my head and they will be removed in 5.0. I do like the named parameters though. I have been wanting those ever since I did some Office Automation stuff.

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

              I finally tried one the other day, then refactored the need away. It's a good tool to have in the toolbox, but I don't expect to use it all that much.

              1 Reply Last reply
              0
              • R realJSOP

                Two words - side effects... What I've noticed is that only ex-VB programmers seem to be excited about optional and named parameters.

                .45 ACP - because shooting twice is just silly
                -----
                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                S Offline
                S Offline
                S Senthil Kumar
                wrote on last edited by
                #27

                John Simmons / outlaw programmer wrote:

                side effects

                Did you mean dependencies between parameters?

                void Method(int x, int y) {}

                int z = 2;
                Method(y : z, x : ++z)

                Regards Senthil _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                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