I wish C# 3.0 had optional parameters
-
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
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
-
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, 2001We 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 usedTheFunc(1)
- yes, the prototype had a void parameter. That's why another call couldn't work in test mode, you had to useTheFunc(1)
first to set test mode! :doh:Join the cool kids - Come fold with us[^]
-
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[^]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.
-
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.
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[^] -
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[^]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.
-
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[^]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.
-
... 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
For one method?! :wtf: You're doing something wrong.
-
... 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
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.
-
For one method?! :wtf: You're doing something wrong.
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
-
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.
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
-
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.
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
-
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, 2001Agreed. 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![^] -
... 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
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
-
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[^]Keith Barrow wrote:
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".
Ahh the 1 billionth post from a developer who knows more than the powers that be who are clearly stupid, you sir win a cookie! ;) But of course from a completely objective standpoint they *are* just toys if you do winform development only; LINQ as we all know serves no useful purpose to the end user which is what it's all about and a wise company always keeps their focus squarely on their end users. I agree with you that a good developer always wants to learn about the bleeding edge but a wise developer always codes with their end users in mind. It's a dilemma no doubt but there are many compelling reasons for a company to stick to .net 2.0 even though it might not attract the best and brightest developers and simply not knowing those reasons doesn't automatically make them wrong.
Let not your mind run on what you lack as much as on what you have already. - Marcus Aurelius
-
Keith Barrow wrote:
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".
Ahh the 1 billionth post from a developer who knows more than the powers that be who are clearly stupid, you sir win a cookie! ;) But of course from a completely objective standpoint they *are* just toys if you do winform development only; LINQ as we all know serves no useful purpose to the end user which is what it's all about and a wise company always keeps their focus squarely on their end users. I agree with you that a good developer always wants to learn about the bleeding edge but a wise developer always codes with their end users in mind. It's a dilemma no doubt but there are many compelling reasons for a company to stick to .net 2.0 even though it might not attract the best and brightest developers and simply not knowing those reasons doesn't automatically make them wrong.
Let not your mind run on what you lack as much as on what you have already. - Marcus Aurelius
Are you a manager by any chance :-)?
John C wrote:
It's a dilemma no doubt but there are many compelling reasons for a company to stick to .net 2.0 even though it might not attract the best and brightest developers and simply not knowing those reasons doesn't automatically make them wrong.
Automatically, no, but with some consideration, I betted on probably. I took time to find out how their business works (as I always do pre-interview), the company was an IT consultancy writing bespoke software, in a market where quick and cheap (and as bug-free as possible as a sadly lesser consideration) tend to win out. The time-to-live on newer versions of C# etc tends to shorter, rival companies using the newer versions would have been able to out-compete as they aren't, for example, taking time to write [invariably buggy from what I've seen] code to get stuff in and out of data sets. I doubt it is in the client's (or user's interest) to have poor quality systems, or systems which take longer to fix. Additionally, the company was a small start-up but already saddled with a lot of the burdens of the bigger umbrella company it was being spun-out from, without the benefits, the worst of both worlds.
John C wrote:
But of course from a completely objective standpoint they *are* just toys if you do winform development only; LINQ as we all know serves no useful purpose to the end user which is what it's all about and a wise company always keeps their focus squarely on their end users
From a completely objective standpoint, computers are just toys, if you are prepared to put up with everything being done on paper. The end users will prefer this, they know how pen & paper works. This is just an argument against progress. The wise company will keep its focus on the squarely on their on whoever decides where the money should be spent when procuring IT rather than the users. If you are selling a game for example, these would be the users, but otherwise keeping the users happy is a secondary, but important, task. As we have to take the world the way it is (as you are quoting Marcus Aurelius - I've read the meditations, quite a work, and infinitely quotable!), we also have to realise that the system that users keeps the users happy isn't always the system the client wants or needs, in a perfect world they would be, but we do not live in one of those.
-
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.
I've done what I usually do, used ".net" interchangeably with "c#" - stupid I know, but I keep doing it anyway , I can be quite wooley-minded at times. :(
Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-A Dead ringer for Kate Winslett[^] -
Are you a manager by any chance :-)?
John C wrote:
It's a dilemma no doubt but there are many compelling reasons for a company to stick to .net 2.0 even though it might not attract the best and brightest developers and simply not knowing those reasons doesn't automatically make them wrong.
Automatically, no, but with some consideration, I betted on probably. I took time to find out how their business works (as I always do pre-interview), the company was an IT consultancy writing bespoke software, in a market where quick and cheap (and as bug-free as possible as a sadly lesser consideration) tend to win out. The time-to-live on newer versions of C# etc tends to shorter, rival companies using the newer versions would have been able to out-compete as they aren't, for example, taking time to write [invariably buggy from what I've seen] code to get stuff in and out of data sets. I doubt it is in the client's (or user's interest) to have poor quality systems, or systems which take longer to fix. Additionally, the company was a small start-up but already saddled with a lot of the burdens of the bigger umbrella company it was being spun-out from, without the benefits, the worst of both worlds.
John C wrote:
But of course from a completely objective standpoint they *are* just toys if you do winform development only; LINQ as we all know serves no useful purpose to the end user which is what it's all about and a wise company always keeps their focus squarely on their end users
From a completely objective standpoint, computers are just toys, if you are prepared to put up with everything being done on paper. The end users will prefer this, they know how pen & paper works. This is just an argument against progress. The wise company will keep its focus on the squarely on their on whoever decides where the money should be spent when procuring IT rather than the users. If you are selling a game for example, these would be the users, but otherwise keeping the users happy is a secondary, but important, task. As we have to take the world the way it is (as you are quoting Marcus Aurelius - I've read the meditations, quite a work, and infinitely quotable!), we also have to realise that the system that users keeps the users happy isn't always the system the client wants or needs, in a perfect world they would be, but we do not live in one of those.
Keith Barrow wrote:
Are you a manager by any chance?
I'm a manager, partner, designer and daily in the trenches developer in a software company that is probably one of the smallest in proportion to the size of our customer base of any I've ever heard of. :) This means I have a responsibility to err on the side of practicality every time when making decisions. We have almost no margin for error or wasted time and because we provide free support as a core value of our company and are such a small company with such a large globally distributed user base we live and die by how happy our customers are and how easily they can use the software without problems.
Keith Barrow wrote:
the company was an IT consultancy writing bespoke software,
Ahhh...I did that once for a while until I came to my senses. It's a wheel I was glad to get off of, I prefer to write software once and sell it over and over again. A far more tranquil business to be in overall.
Keith Barrow wrote:
as they aren't, for example, taking time to write [invariably buggy from what I've seen] code to get stuff in and out of data sets.
I can't speak for what you've seen but I strongly disagree that it's invariably buggy if you are inferring that LINQ somehow results in magically less buggy code through some power of sympathetic magic previously unknown. I feel that LINQ results in dense hard to understand and maintain code that requires developers to maintain it who tend to be of the kind that lean towards the ivory tower attitude towards software development that are in short the sort of people that are too much trouble to hire in my admittedly limited experience. ;)
Keith Barrow wrote:
The wise company will keep its focus on the squarely on their on whoever decides where the money should be spent when procuring IT rather than the users.
This may represent an attitude born of doing a lot of contract software development, my opinions come from the different world of making commercial off the shelf business software. Personally I think in either case software development is a craft and art form and good design is critical and it would gall me to make software that was less easy and enjoyable to use, software that wasn't task oriented merely to make some bean counter happy. I've found in life that w
-
I've done what I usually do, used ".net" interchangeably with "c#" - stupid I know, but I keep doing it anyway , I can be quite wooley-minded at times. :(
Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-A Dead ringer for Kate Winslett[^]Thought so, but when I see a chain I yank it -- pedantry never sleeps. :-D
-
... 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
It does! It's a compiler thing.. the .NET4 compiler makes optional parameter in C#3 code. I have VS2010, and our project is still ( :(( ) in .NET3.5, but I use optional parameter all the time, thanks to VS2010, SDK .NET4!!! :)
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
Thought so, but when I see a chain I yank it -- pedantry never sleeps. :-D
PIEBALDconsult wrote:
pedantry never sleeps.
LOL!
Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-A Dead ringer for Kate Winslett[^]