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. I wish C# 3.0 had optional parameters

I wish C# 3.0 had optional parameters

Scheduled Pinned Locked Moved The Lounge
csharpdotnetcomgraphics
36 Posts 14 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.
  • C Cesar de Souza

    ... it is like the 218283th overload I am writing, and it only grows exponentially! :( Oh, and for the down-voters - this was meant to be a joke :doh:

    Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

    Mike HankeyM Offline
    Mike HankeyM Offline
    Mike Hankey
    wrote on last edited by
    #3

    Check this out [^]

    Artificial Intelligence is no match for Natural Stupidity. http://www.hq4thmarinescomm.com[^] My Site

    C 1 Reply Last reply
    0
    • realJSOPR realJSOP

      When I was coding in C++, I learned that, more often than not, default parameters prevent you from making the code as flexible without sometimes dire side effects. I won't use them in C# 4.0 simply because I think they're not useful enough to override the aforementioned side-effects.

      .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

      Mike HankeyM Offline
      Mike HankeyM Offline
      Mike Hankey
      wrote on last edited by
      #4

      I found them very useful in certain cases such as a bool that 9 times out of 10 is one value and instead of having to add that parameter for every invocation can only use in cases where needed. Also if refactoring and you add a bool instead of going back and changing every single call you can just change as needed. Just my two sense.

      Artificial Intelligence is no match for Natural Stupidity. http://www.hq4thmarinescomm.com[^] My Site

      1 Reply Last reply
      0
      • C Cesar de Souza

        ... it is like the 218283th overload I am writing, and it only grows exponentially! :( Oh, and for the down-voters - this was meant to be a joke :doh:

        Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #5

        I'm going to throw this out to you out as a suggestion: Just upgrade to .net 4.0 if possible, you should have unit tests in place to see if anything breaks :-), if everything is OK (and you don't have any weird user-machine requirements) then upgrade. I withdrew from an interview (the only time I have ever done this) about a year and a half ago because they were using .net 2.0 and had no plans to updgrade. They said that they understood that developers always wanted to "play with new toys" I challenged this (politely of course), I'd already shipped .net 3.5 stuff and the improvements to over .net 2.0 were compelling: LINQ, performance, WCF, WPF etc, not just "toys". I also pointed out that any decent dev will want to move onto the new platorm ASAP (OK, the "P" is important) and you [presumably] want to hire decent dev. I spoke to the two devs on the interview panel, one wasn't interested in learning new stuff, the other just seemed hidebound by corporate sclerosis and managerial interference. The stupid thing was this was a "startup" but under an umbrella company (itself not huge) that insisted on .net 2.0. I think I even said the company won't remain competative in the market for more than a few years.

        Sort of a cross between Lawrence of Arabia and Dilbert.[^]
        -Or-A Dead ringer for Kate Winslett[^]

        C D M D 4 Replies Last reply
        0
        • realJSOPR realJSOP

          When I was coding in C++, I learned that, more often than not, default parameters prevent you from making the code as flexible without sometimes dire side effects. I won't use them in C# 4.0 simply because I think they're not useful enough to override the aforementioned side-effects.

          .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

          C Offline
          C Offline
          Cesar de Souza
          wrote on last edited by
          #6

          I do agree with you. But I do also believe that any feature can be equally misused. Just because it can be misused doesn't mean a feature should be avoided at all costs, it just means that it should be used carefully ;P

          Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

          1 Reply Last reply
          0
          • K Keith Barrow

            I'm going to throw this out to you out as a suggestion: Just upgrade to .net 4.0 if possible, you should have unit tests in place to see if anything breaks :-), if everything is OK (and you don't have any weird user-machine requirements) then upgrade. I withdrew from an interview (the only time I have ever done this) about a year and a half ago because they were using .net 2.0 and had no plans to updgrade. They said that they understood that developers always wanted to "play with new toys" I challenged this (politely of course), I'd already shipped .net 3.5 stuff and the improvements to over .net 2.0 were compelling: LINQ, performance, WCF, WPF etc, not just "toys". I also pointed out that any decent dev will want to move onto the new platorm ASAP (OK, the "P" is important) and you [presumably] want to hire decent dev. I spoke to the two devs on the interview panel, one wasn't interested in learning new stuff, the other just seemed hidebound by corporate sclerosis and managerial interference. The stupid thing was this was a "startup" but under an umbrella company (itself not huge) that insisted on .net 2.0. I think I even said the company won't remain competative in the market for more than a few years.

            Sort of a cross between Lawrence of Arabia and Dilbert.[^]
            -Or-A Dead ringer for Kate Winslett[^]

            C Offline
            C Offline
            Cesar de Souza
            wrote on last edited by
            #7

            Wow, seems a cool thing to say in a interview :D I do have plans to upgrade, but since this code is meant to be used by others, I just can't assume everyone will have upgraded to VS2010 already. But I do appreciate your advice!

            Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

            1 Reply Last reply
            0
            • Mike HankeyM Mike Hankey

              Check this out [^]

              Artificial Intelligence is no match for Natural Stupidity. http://www.hq4thmarinescomm.com[^] My Site

              C Offline
              C Offline
              Cesar de Souza
              wrote on last edited by
              #8

              Are you referring to __arglist ? Well, I was just ranting because I wasn't willing to clobber the code with several overloads just for the sake of user experience. Using a undocumented way to pass arguments to a function would totally blow up any expectations of user-friendness :P

              Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

              modified on Sunday, December 5, 2010 8:43 AM

              Mike HankeyM 1 Reply Last reply
              0
              • C Cesar de Souza

                Are you referring to __arglist ? Well, I was just ranting because I wasn't willing to clobber the code with several overloads just for the sake of user experience. Using a undocumented way to pass arguments to a function would totally blow up any expectations of user-friendness :P

                Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

                modified on Sunday, December 5, 2010 8:43 AM

                Mike HankeyM Offline
                Mike HankeyM Offline
                Mike Hankey
                wrote on last edited by
                #9

                Only a link...if it suits your needs use it if it doesn't sh%tcan it.

                Artificial Intelligence is no match for Natural Stupidity. http://www.hq4thmarinescomm.com[^] My Site

                C 1 Reply Last reply
                0
                • Mike HankeyM Mike Hankey

                  Only a link...if it suits your needs use it if it doesn't sh%tcan it.

                  Artificial Intelligence is no match for Natural Stupidity. http://www.hq4thmarinescomm.com[^] My Site

                  C Offline
                  C Offline
                  Cesar de Souza
                  wrote on last edited by
                  #10

                  I am grateful for the link - I wasn't aware of some of the functions mentioned there. Thanks :thumbsup:

                  Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

                  B 1 Reply Last reply
                  0
                  • realJSOPR realJSOP

                    When I was coding in C++, I learned that, more often than not, default parameters prevent you from making the code as flexible without sometimes dire side effects. I won't use them in C# 4.0 simply because I think they're not useful enough to override the aforementioned side-effects.

                    .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

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #11

                    We got some code at work where the prototype was hidden away somewhere so the engineer using it followed the code for examples. TheFunc(false); in some places, TheFunc(true); in others. But there was one line of test code which used TheFunc(1) - yes, the prototype had a void parameter. That's why another call couldn't work in test mode, you had to use TheFunc(1) first to set test mode! :doh:

                    Join the cool kids - Come fold with us[^]

                    1 Reply Last reply
                    0
                    • K Keith Barrow

                      I'm going to throw this out to you out as a suggestion: Just upgrade to .net 4.0 if possible, you should have unit tests in place to see if anything breaks :-), if everything is OK (and you don't have any weird user-machine requirements) then upgrade. I withdrew from an interview (the only time I have ever done this) about a year and a half ago because they were using .net 2.0 and had no plans to updgrade. They said that they understood that developers always wanted to "play with new toys" I challenged this (politely of course), I'd already shipped .net 3.5 stuff and the improvements to over .net 2.0 were compelling: LINQ, performance, WCF, WPF etc, not just "toys". I also pointed out that any decent dev will want to move onto the new platorm ASAP (OK, the "P" is important) and you [presumably] want to hire decent dev. I spoke to the two devs on the interview panel, one wasn't interested in learning new stuff, the other just seemed hidebound by corporate sclerosis and managerial interference. The stupid thing was this was a "startup" but under an umbrella company (itself not huge) that insisted on .net 2.0. I think I even said the company won't remain competative in the market for more than a few years.

                      Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                      -Or-A Dead ringer for Kate Winslett[^]

                      D Offline
                      D Offline
                      Daniel Grunwald
                      wrote on last edited by
                      #12

                      You don't need to upgrade to .NET 4.0 in order to use optional parameters. You only need to upgrade to C# 4.0. It's perfectly possible to use VS2010 to create a .NET 2.0 application using optional parameters, LINQ, etc.

                      K 1 Reply Last reply
                      0
                      • D Daniel Grunwald

                        You don't need to upgrade to .NET 4.0 in order to use optional parameters. You only need to upgrade to C# 4.0. It's perfectly possible to use VS2010 to create a .NET 2.0 application using optional parameters, LINQ, etc.

                        K Offline
                        K Offline
                        Keith Barrow
                        wrote on last edited by
                        #13

                        Daniel Grunwald wrote:

                        It's perfectly possible to use VS2010 to create a .NET 2.0 application using optional parameters, LINQ, etc.

                        How (without downgrading to VB for the optional params :-))??????????? As for the LINQ, the syntax was only added in .net 3.5, I'd be surprised whether LINQ would work in .net 2.0 world (.net 3 would make more sense). I'd also question why you'd want to do that, seems like upwind urination to me.

                        Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                        -Or-A Dead ringer for Kate Winslett[^]

                        D P 2 Replies Last reply
                        0
                        • K Keith Barrow

                          Daniel Grunwald wrote:

                          It's perfectly possible to use VS2010 to create a .NET 2.0 application using optional parameters, LINQ, etc.

                          How (without downgrading to VB for the optional params :-))??????????? As for the LINQ, the syntax was only added in .net 3.5, I'd be surprised whether LINQ would work in .net 2.0 world (.net 3 would make more sense). I'd also question why you'd want to do that, seems like upwind urination to me.

                          Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                          -Or-A Dead ringer for Kate Winslett[^]

                          D Offline
                          D Offline
                          Daniel Grunwald
                          wrote on last edited by
                          #14

                          You're confusing the compiler and the runtime. (btw: so is the thread starter. "C# 3.5" doesn't exist, there are only "C# 2.0","C# 3.0","C# 4.0" and ".NET 2.0",".NET 3.0",".NET 3.5",".NET 4.0") When using C# 4.0 to target .NET 2.0, you can use optional parameters, extension methods, LINQ, even generic variance. Remember, LINQ is just a compiler feature that translates query expressions to method calls. If you target .NET 2.0, the Select/Where/etc. methods don't exist in the framework, but it's perfectly possible to write your own. Or just include Mono's copy of those methods in your app. This works because the file format of .NET assemblies (metadata, IL instructions, etc.) hasn't changed since .NET 2.0. The why is simple: you want to target the .NET 2.0 framework already installed on millions of machines, but still want to use the new language features.

                          C 1 Reply Last reply
                          0
                          • K Keith Barrow

                            Daniel Grunwald wrote:

                            It's perfectly possible to use VS2010 to create a .NET 2.0 application using optional parameters, LINQ, etc.

                            How (without downgrading to VB for the optional params :-))??????????? As for the LINQ, the syntax was only added in .net 3.5, I'd be surprised whether LINQ would work in .net 2.0 world (.net 3 would make more sense). I'd also question why you'd want to do that, seems like upwind urination to me.

                            Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                            -Or-A Dead ringer for Kate Winslett[^]

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

                            Keith Barrow wrote:

                            the syntax was only added in .net 3.5

                            .net doesn't have syntax; the languages does. You can target earlier frameworks with the newer language compiler.

                            K 1 Reply Last reply
                            0
                            • C Cesar de Souza

                              ... it is like the 218283th overload I am writing, and it only grows exponentially! :( Oh, and for the down-voters - this was meant to be a joke :doh:

                              Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

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

                              For one method?! :wtf: You're doing something wrong.

                              C 1 Reply Last reply
                              0
                              • C Cesar de Souza

                                ... it is like the 218283th overload I am writing, and it only grows exponentially! :( Oh, and for the down-voters - this was meant to be a joke :doh:

                                Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

                                D Offline
                                D Offline
                                Daniel Grunwald
                                wrote on last edited by
                                #17

                                If you can't use the C# 4.0 compiler (you can still target .NET 3.5 if you do!): Use a parameter class.

                                MyMethod(new MyMethodArguments { X = 1, Z = 3 })

                                A parameter class has the advantage that you can add parameters and/or change the default values without breaking binary compatibility. With optional parameters, your clients have to recompile every time you change the method declaration.

                                C 1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  For one method?! :wtf: You're doing something wrong.

                                  C Offline
                                  C Offline
                                  Cesar de Souza
                                  wrote on last edited by
                                  #18

                                  I was just being dramatic :-D But no, it was not just for one method. :)

                                  Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

                                  1 Reply Last reply
                                  0
                                  • D Daniel Grunwald

                                    If you can't use the C# 4.0 compiler (you can still target .NET 3.5 if you do!): Use a parameter class.

                                    MyMethod(new MyMethodArguments { X = 1, Z = 3 })

                                    A parameter class has the advantage that you can add parameters and/or change the default values without breaking binary compatibility. With optional parameters, your clients have to recompile every time you change the method declaration.

                                    C Offline
                                    C Offline
                                    Cesar de Souza
                                    wrote on last edited by
                                    #19

                                    Well, even if you create optional parameters in VS2010 targeting .NET 3.5, users willing to use those methods under .NET 3.5 using C# 3.0 (i.e. VS2008) will not be able to take advantage of it, because the compiler does not generate overloaded constructors in this case. But using a parameter class is nice, thanks for the idea. :)

                                    Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

                                    modified on Sunday, December 5, 2010 11:26 AM

                                    1 Reply Last reply
                                    0
                                    • D Daniel Grunwald

                                      You're confusing the compiler and the runtime. (btw: so is the thread starter. "C# 3.5" doesn't exist, there are only "C# 2.0","C# 3.0","C# 4.0" and ".NET 2.0",".NET 3.0",".NET 3.5",".NET 4.0") When using C# 4.0 to target .NET 2.0, you can use optional parameters, extension methods, LINQ, even generic variance. Remember, LINQ is just a compiler feature that translates query expressions to method calls. If you target .NET 2.0, the Select/Where/etc. methods don't exist in the framework, but it's perfectly possible to write your own. Or just include Mono's copy of those methods in your app. This works because the file format of .NET assemblies (metadata, IL instructions, etc.) hasn't changed since .NET 2.0. The why is simple: you want to target the .NET 2.0 framework already installed on millions of machines, but still want to use the new language features.

                                      C Offline
                                      C Offline
                                      Cesar de Souza
                                      wrote on last edited by
                                      #20

                                      I've just corrected the title - I am aware of the difference, but it totally slipped away into the title - thanks for noticing it :) Anyways, even if you create optional parameters in VS2010 targeting .NET 3.5, users willing to use those methods under .NET 3.5 using C# 3.0 (i.e. VS2008) will not be able to take advantage of it. For optional parameters, the compiler does not seem to create additional overloads for you. :sigh:

                                      Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

                                      1 Reply Last reply
                                      0
                                      • realJSOPR realJSOP

                                        When I was coding in C++, I learned that, more often than not, default parameters prevent you from making the code as flexible without sometimes dire side effects. I won't use them in C# 4.0 simply because I think they're not useful enough to override the aforementioned side-effects.

                                        .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

                                        G Offline
                                        G Offline
                                        Gary R Wheeler
                                        wrote on last edited by
                                        #21

                                        Agreed. If there are so many alternatives that you're building that many overrides, it sounds like you've got a more fundamental problem. Too much behavior is being specified by the user of a method. My approach to this sort of problem has been to refactor the method into a class of its own, define the method itself as virtual (abstract in C#), and implement each variation as a derived class.

                                        Software Zen: delete this;
                                        Fold With Us![^]

                                        1 Reply Last reply
                                        0
                                        • C Cesar de Souza

                                          ... it is like the 218283th overload I am writing, and it only grows exponentially! :( Oh, and for the down-voters - this was meant to be a joke :doh:

                                          Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

                                          E Offline
                                          E Offline
                                          Ennis Ray Lynch Jr
                                          wrote on last edited by
                                          #22

                                          I don't particularly like optional parameters myself. Given the choice I would choice the ability to overload over the ability to have optional parameters any day of the week.

                                          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                                          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