Default values in method declaration
-
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?
void MyFun(int nParam1)
{
MyFun(nParam1, 0);
}
void MyFun(int nParam1, int nDefault)
{
}In other words, No...
-
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?
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.
-
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.
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
-
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
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++ 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?
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!
-
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.
lol, beat me to it ;P James Simplicity Rules!
-
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.
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
-
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!
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
-
void MyFun(int nParam1)
{
MyFun(nParam1, 0);
}
void MyFun(int nParam1, int nDefault)
{
}In other words, No...
-
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.
-
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++ 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?
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
-
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
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
-
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.
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
-
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
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
-
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
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
-
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
-
Thank you for information. This is quite reasonable (but sometimes default parameters are very usefull :) Tomiga
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