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. Default values in method declaration

Default values in method declaration

Scheduled Pinned Locked Moved C#
csharpc++tutorialquestion
19 Posts 6 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.
  • T tomiga

    C++ allows to use default values for parameters e.g. void MyFun(int nParam1, nDefault = 0); Is it possible in C#? If yes, how to do it?

    J Offline
    J Offline
    James T Johnson
    wrote on last edited by
    #6

    Here is what Eric Gunnerson had to say about the subject on the aspngcs list. [The discussion was regarding optional parameters in VB, but they are essentially the same thing] I'd like to try to share a bit of the language designer perspective to this. When we look at features, the question that we try to answer is: Is the utility that this feature adds worth the additional complexity in the language? When we did this for C#, there were a number of cases where the tradeoff isn't clear-cut. Our default decision is not to add a feature, because simplicity is also a feature. To put this another way, C# is already more complex than we'd like it to be, so additional features have to be really useful to meet the cut. Then later, I appreciate the feedback. I'm not sure you have to persuade me - I'm pretty much on the fence on this one. You'd really have to persuade Anders... I can give you a bit more perspective on this issue. In C++, default parameters don't version well, because the default value ends up in the client code rather than your library. I think that's a pretty serious drawback, and I don't think it's the right approach for C#. One other thing we've considered doing was having the compiler take a definition like: public void (string s1, string s2 = null, string s3 = null) { } and generate the other two overloads. IIRC, we're planning on discussing this so more, but I don't know what the outcome will be. If anyone wants I can e-mail them the thread (titled "Optional Parameters in C#" for those on the list). James Simplicity Rules!

    C T 2 Replies Last reply
    0
    • A Andy Smith

      we've had many talks with Eric Gunnerson about this on the aspfriends.com email lists. you may disagree with it... but the reason they didn't implement it was because they favored ( by a small margin ) overall simplicity against More Features© however, we have been fairly successfull at giving good arguments for it... and they are still considering it for v2.

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #7

      lol, beat me to it ;P James Simplicity Rules!

      1 Reply Last reply
      0
      • A Andy Smith

        we've had many talks with Eric Gunnerson about this on the aspfriends.com email lists. you may disagree with it... but the reason they didn't implement it was because they favored ( by a small margin ) overall simplicity against More Features© however, we have been fairly successfull at giving good arguments for it... and they are still considering it for v2.

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #8

        The more I use C#, the more it treats me like an idiot. It's clear to me they went for simplicity, for the benefit of people coming over from VB, I suspect. Thanks for the insight. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

        1 Reply Last reply
        0
        • J James T Johnson

          Here is what Eric Gunnerson had to say about the subject on the aspngcs list. [The discussion was regarding optional parameters in VB, but they are essentially the same thing] I'd like to try to share a bit of the language designer perspective to this. When we look at features, the question that we try to answer is: Is the utility that this feature adds worth the additional complexity in the language? When we did this for C#, there were a number of cases where the tradeoff isn't clear-cut. Our default decision is not to add a feature, because simplicity is also a feature. To put this another way, C# is already more complex than we'd like it to be, so additional features have to be really useful to meet the cut. Then later, I appreciate the feedback. I'm not sure you have to persuade me - I'm pretty much on the fence on this one. You'd really have to persuade Anders... I can give you a bit more perspective on this issue. In C++, default parameters don't version well, because the default value ends up in the client code rather than your library. I think that's a pretty serious drawback, and I don't think it's the right approach for C#. One other thing we've considered doing was having the compiler take a definition like: public void (string s1, string s2 = null, string s3 = null) { } and generate the other two overloads. IIRC, we're planning on discussing this so more, but I don't know what the outcome will be. If anyone wants I can e-mail them the thread (titled "Optional Parameters in C#" for those on the list). James Simplicity Rules!

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #9

          James T. Johnson wrote: To put this another way, C# is already more complex than we'd like it to be, :omg: :omg: :omg: Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

          1 Reply Last reply
          0
          • N Neil Van Note

            void MyFun(int nParam1)
            {
            MyFun(nParam1, 0);
            }
            void MyFun(int nParam1, int nDefault)
            {
            }

            In other words, No...

            T Offline
            T Offline
            tomiga
            wrote on last edited by
            #10

            Clever ;)

            1 Reply Last reply
            0
            • T Tom Archer

              Default parameters are not inherently supported in C#. You need to define an overloaded version of the method that doesn't require the parameter (that defaults it) and call the version that contains all the parameters. Before you say it, I agree that it sucks :) Cheers, Tom Archer Author, Inside C# A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.

              T Offline
              T Offline
              tomiga
              wrote on last edited by
              #11

              :)

              1 Reply Last reply
              0
              • J James T Johnson

                Here is what Eric Gunnerson had to say about the subject on the aspngcs list. [The discussion was regarding optional parameters in VB, but they are essentially the same thing] I'd like to try to share a bit of the language designer perspective to this. When we look at features, the question that we try to answer is: Is the utility that this feature adds worth the additional complexity in the language? When we did this for C#, there were a number of cases where the tradeoff isn't clear-cut. Our default decision is not to add a feature, because simplicity is also a feature. To put this another way, C# is already more complex than we'd like it to be, so additional features have to be really useful to meet the cut. Then later, I appreciate the feedback. I'm not sure you have to persuade me - I'm pretty much on the fence on this one. You'd really have to persuade Anders... I can give you a bit more perspective on this issue. In C++, default parameters don't version well, because the default value ends up in the client code rather than your library. I think that's a pretty serious drawback, and I don't think it's the right approach for C#. One other thing we've considered doing was having the compiler take a definition like: public void (string s1, string s2 = null, string s3 = null) { } and generate the other two overloads. IIRC, we're planning on discussing this so more, but I don't know what the outcome will be. If anyone wants I can e-mail them the thread (titled "Optional Parameters in C#" for those on the list). James Simplicity Rules!

                T Offline
                T Offline
                tomiga
                wrote on last edited by
                #12

                thx for explanation

                1 Reply Last reply
                0
                • T tomiga

                  C++ allows to use default values for parameters e.g. void MyFun(int nParam1, nDefault = 0); Is it possible in C#? If yes, how to do it?

                  T Offline
                  T Offline
                  Tom Archer
                  wrote on last edited by
                  #13

                  I just spoke with Joe Nalewabau (Product Manager for C#) and asked him why default params were not included in the language. I figured you might want to hear the "inside story": [Joe Nalewabau] - Default parameters were not added due to the versioning issues associated with them. Essentially when you add default parameters the compiler essentially burns the default value in to the callers code when the code is compiled. This means that if you change the default values all clients must be recompiled to take advantage of the new value - rather than have the class library itself decide what the default values should be. Hope this helps. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                  T 1 Reply Last reply
                  0
                  • C Christian Graus

                    How could they leave out something so trivial to impliment, and so useful ? Templates I can understand given that Microsoft are yet to release a worthwhile implimentation of templates on any platform, but this just blows. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

                    T Offline
                    T Offline
                    Tom Archer
                    wrote on last edited by
                    #14

                    I followed up on this with Joe Nalewabau. Here is what he had to say about this. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                    C 1 Reply Last reply
                    0
                    • A Andy Smith

                      we've had many talks with Eric Gunnerson about this on the aspfriends.com email lists. you may disagree with it... but the reason they didn't implement it was because they favored ( by a small margin ) overall simplicity against More Features© however, we have been fairly successfull at giving good arguments for it... and they are still considering it for v2.

                      T Offline
                      T Offline
                      Tom Archer
                      wrote on last edited by
                      #15

                      This is actually not the case. According to the C# Product Manager it had more to do with versioning than anything. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                      1 Reply Last reply
                      0
                      • T Tom Archer

                        I followed up on this with Joe Nalewabau. Here is what he had to say about this. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #16

                        It's a percieved issue with distributed libraries ? Given that people are talking about doing the same thing via two functions, the hard coded values are still there, we're just finding ways around the limitation in C#. But of course, I should be telling Joe this :-) Thanks for the info. BTW, Amazon shipping my copy of Inside C# on the weekend, so the delay was not that bad. Soon I will actually start learning me some C# instead of flailing around blindly. The Petzold book is helpful when I have a UI issue, but the other book I have, I have quickly outgrown. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

                        T 1 Reply Last reply
                        0
                        • C Christian Graus

                          It's a percieved issue with distributed libraries ? Given that people are talking about doing the same thing via two functions, the hard coded values are still there, we're just finding ways around the limitation in C#. But of course, I should be telling Joe this :-) Thanks for the info. BTW, Amazon shipping my copy of Inside C# on the weekend, so the delay was not that bad. Soon I will actually start learning me some C# instead of flailing around blindly. The Petzold book is helpful when I have a UI issue, but the other book I have, I have quickly outgrown. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

                          T Offline
                          T Offline
                          Tom Archer
                          wrote on last edited by
                          #17

                          Christian Graus wrote: It's a percieved issue with distributed libraries ? Given that people are talking about doing the same thing via two functions, the hard coded values are still there, we're just finding ways around the limitation in C#. Agreed. There were many situations where the reason for omitting something from C# that most C++ developers would consider important was due to versioning :( Christian Graus wrote: But of course, I should be telling Joe this I'm sure he's heard it :) Christian Graus wrote: BTW, Amazon shipping my copy of Inside C# on the weekend, so the delay was not that bad Very cool! I'll be here for any questions. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                          1 Reply Last reply
                          0
                          • T Tom Archer

                            I just spoke with Joe Nalewabau (Product Manager for C#) and asked him why default params were not included in the language. I figured you might want to hear the "inside story": [Joe Nalewabau] - Default parameters were not added due to the versioning issues associated with them. Essentially when you add default parameters the compiler essentially burns the default value in to the callers code when the code is compiled. This means that if you change the default values all clients must be recompiled to take advantage of the new value - rather than have the class library itself decide what the default values should be. Hope this helps. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                            T Offline
                            T Offline
                            tomiga
                            wrote on last edited by
                            #18

                            Thank you for information. This is quite reasonable (but sometimes default parameters are very usefull :) Tomiga

                            T 1 Reply Last reply
                            0
                            • T tomiga

                              Thank you for information. This is quite reasonable (but sometimes default parameters are very usefull :) Tomiga

                              T Offline
                              T Offline
                              Tom Archer
                              wrote on last edited by
                              #19

                              I'm with you. I'd rather have the default params. As Christian stated, people are just going to inject work-arounds to the lack of support for default params that are cause the same problems that resulted in their omission to begin with :) Cheers, Tom Archer Author - Inside C#, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                              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