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 argument values

Default argument values

Scheduled Pinned Locked Moved C#
csharpcomquestion
7 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.
  • A Offline
    A Offline
    Alexandru Savescu
    wrote on last edited by
    #1

    Hi! I am quite new to C# and have noticed that one cannot give default values to arguments. Any idea why this is not implementend in C#? :confused: :confused: :confused: Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

    D 1 Reply Last reply
    0
    • A Alexandru Savescu

      Hi! I am quite new to C# and have noticed that one cannot give default values to arguments. Any idea why this is not implementend in C#? :confused: :confused: :confused: Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

      D Offline
      D Offline
      Daniel Turini
      wrote on last edited by
      #2

      Alexandru Savescu wrote: Any idea why this is not implementend in C#? As I've read, this leads to versioning problems. Where does the default is defined? On the client or in the interface implementation?


      It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)

      A 1 Reply Last reply
      0
      • D Daniel Turini

        Alexandru Savescu wrote: Any idea why this is not implementend in C#? As I've read, this leads to versioning problems. Where does the default is defined? On the client or in the interface implementation?


        It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)

        A Offline
        A Offline
        Alexandru Savescu
        wrote on last edited by
        #3

        I think I was not clear enough. I meant default argument values to functions like in C++:

        void f (int x = 0)
        {
        printf ("%d", x);
        }

        Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

        D 1 Reply Last reply
        0
        • A Alexandru Savescu

          I think I was not clear enough. I meant default argument values to functions like in C++:

          void f (int x = 0)
          {
          printf ("%d", x);
          }

          Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

          D Offline
          D Offline
          Daniel Turini
          wrote on last edited by
          #4

          Alexandru Savescu wrote: I think I was not clear enough. No, you were. I wasn't, sorry. This page explains it better than me: " Because IL always calls a method passing a complete set of arguments, the value of an absent argument is determined at compile time, not at run time. This has versioning consequences. For example, say a ComponentAssembly.dll assembly contains a method that has an optional Int32 argument whose default value is 5. Now imagine an AppAssembly.exe assembly that contains a call to this method and the optional argument isn’t specified. When compiling AppAssembly.exe, the compiler emits IL code that passes 5 to the method. Let’s say that the method in ComponentAssembly.dll has its optional parameter’s default value changed to 123 and the assembly is rebuilt. If the AppAssembly.exe assembly isn’t rebuilt, then at run time, 5 is passed to the method—the new default value (123) isn’t passed to the method unless the code in AppAssembly.exe is also recompiled. " http://www.microsoft.com/mspress/books/sampchap/6199e.asp[^]


          It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)

          A A 2 Replies Last reply
          0
          • D Daniel Turini

            Alexandru Savescu wrote: I think I was not clear enough. No, you were. I wasn't, sorry. This page explains it better than me: " Because IL always calls a method passing a complete set of arguments, the value of an absent argument is determined at compile time, not at run time. This has versioning consequences. For example, say a ComponentAssembly.dll assembly contains a method that has an optional Int32 argument whose default value is 5. Now imagine an AppAssembly.exe assembly that contains a call to this method and the optional argument isn’t specified. When compiling AppAssembly.exe, the compiler emits IL code that passes 5 to the method. Let’s say that the method in ComponentAssembly.dll has its optional parameter’s default value changed to 123 and the assembly is rebuilt. If the AppAssembly.exe assembly isn’t rebuilt, then at run time, 5 is passed to the method—the new default value (123) isn’t passed to the method unless the code in AppAssembly.exe is also recompiled. " http://www.microsoft.com/mspress/books/sampchap/6199e.asp[^]


            It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)

            A Offline
            A Offline
            Alexandru Savescu
            wrote on last edited by
            #5

            Thanks, that clarifies it. Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

            1 Reply Last reply
            0
            • D Daniel Turini

              Alexandru Savescu wrote: I think I was not clear enough. No, you were. I wasn't, sorry. This page explains it better than me: " Because IL always calls a method passing a complete set of arguments, the value of an absent argument is determined at compile time, not at run time. This has versioning consequences. For example, say a ComponentAssembly.dll assembly contains a method that has an optional Int32 argument whose default value is 5. Now imagine an AppAssembly.exe assembly that contains a call to this method and the optional argument isn’t specified. When compiling AppAssembly.exe, the compiler emits IL code that passes 5 to the method. Let’s say that the method in ComponentAssembly.dll has its optional parameter’s default value changed to 123 and the assembly is rebuilt. If the AppAssembly.exe assembly isn’t rebuilt, then at run time, 5 is passed to the method—the new default value (123) isn’t passed to the method unless the code in AppAssembly.exe is also recompiled. " http://www.microsoft.com/mspress/books/sampchap/6199e.asp[^]


              It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)

              A Offline
              A Offline
              Andy Smith
              wrote on last edited by
              #6

              One way to get around this issue would be for the c# compiler to auto-generate the necesary method overloads for optional params. for instance: public void Foo(Int32 i = 5){...} would get compiled to: public void Foo(Int32 i) {...} public void Foo() { Foo(5); } This would allow the value of the optional paramter to be stored in the correct assembly for versioning issues. It's not a perfect solution, but it works pretty well.

              N 1 Reply Last reply
              0
              • A Andy Smith

                One way to get around this issue would be for the c# compiler to auto-generate the necesary method overloads for optional params. for instance: public void Foo(Int32 i = 5){...} would get compiled to: public void Foo(Int32 i) {...} public void Foo() { Foo(5); } This would allow the value of the optional paramter to be stored in the correct assembly for versioning issues. It's not a perfect solution, but it works pretty well.

                N Offline
                N Offline
                Nick Parker
                wrote on last edited by
                #7

                Andy Smith wrote: public void Foo(Int32 i) {...} public void Foo() { Foo(5); } This would allow the value of the optional paramter to be stored in the correct assembly for versioning issues. Nice idea. :) -Nick Parker

                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