Default argument values
-
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!
-
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!
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)
-
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)
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!
-
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!
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)
-
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)
Thanks, that clarifies it. Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
-
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)
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.
-
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.
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