Optional Parameter in C#
-
Hi all... Can there be an optional or default parameter in C#? If yes what would be the syntax of the same?
You can define a params collection, otherwise you need to write several methods like this": void DoSomething(int i, int j, in k) { } void DoSomething(int i, int j) { DoSomething(i, j, 5); } void DoSomething(int i) { DoSomething(i, 2, 5); } Obviously, this is less than ideal. Christian Graus - Microsoft MVP - C++
-
You can define a params collection, otherwise you need to write several methods like this": void DoSomething(int i, int j, in k) { } void DoSomething(int i, int j) { DoSomething(i, j, 5); } void DoSomething(int i) { DoSomething(i, 2, 5); } Obviously, this is less than ideal. Christian Graus - Microsoft MVP - C++
Ya...one option is of method overloading, but when we have more number of params to be kept as optional one has to type a lot. Is there any other work around as in VB.NET we have an optional keyword which specifies the parameter is optional and with the specified default value.
-
Ya...one option is of method overloading, but when we have more number of params to be kept as optional one has to type a lot. Is there any other work around as in VB.NET we have an optional keyword which specifies the parameter is optional and with the specified default value.
To reiterate: no. I recently found out that VB.NET supports optional parameters and C# does not. Until then, I thought that VB.NET was always the loser, I guess that's not the case. Having said that, I think that an interface where a method takes 20 parameters and 10 are optional is just sloppy coding anyhow. Christian Graus - Microsoft MVP - C++
-
To reiterate: no. I recently found out that VB.NET supports optional parameters and C# does not. Until then, I thought that VB.NET was always the loser, I guess that's not the case. Having said that, I think that an interface where a method takes 20 parameters and 10 are optional is just sloppy coding anyhow. Christian Graus - Microsoft MVP - C++
Here is entry about why C# doesn't have default parameters from C# Frequently Asked Questions. http://blogs.msdn.com/csharpfaq/archive/2004/03/07/85556.aspx[^] Surprising perspective, isn't it? David
-
To reiterate: no. I recently found out that VB.NET supports optional parameters and C# does not. Until then, I thought that VB.NET was always the loser, I guess that's not the case. Having said that, I think that an interface where a method takes 20 parameters and 10 are optional is just sloppy coding anyhow. Christian Graus - Microsoft MVP - C++
C# vs VB is all a matter of style. If you look closely at the features of both languages, you'll see that VB offers about as many features that C# doesn't have that C# offers that VB doesn't have. C#: - allows unsafe code (I've yet to meet anyone who does this in C# though) - has the 'as' operator (but VB has it in 2005 - 'TryCast') - allows assignments within expressions (e.g., if ((x = y.Value) == 2)) - operator overloading VB: - has optional parameters - allows types within interfaces (although I've never wished I had this in C#) - has a very flexible Select construct (much, much more flexible than 'switch') - has the "When" filter for catch block headers (ok - I'm really stretching now...) And this one is a doubtful benefit of VB: it gives you half a dozen ways to do a lot of things - some regard it as a benefit, while others hate it since it makes it harder to produce standard code in teams. David Anton Tangible Software Solutions www.tangiblesoftwaresolutions.com Home of: Instant C#: VB.NET to C# Converter Instant VB: C# to VB.NET Converter Instant J#: VB.NET to J# Converter
-
C# vs VB is all a matter of style. If you look closely at the features of both languages, you'll see that VB offers about as many features that C# doesn't have that C# offers that VB doesn't have. C#: - allows unsafe code (I've yet to meet anyone who does this in C# though) - has the 'as' operator (but VB has it in 2005 - 'TryCast') - allows assignments within expressions (e.g., if ((x = y.Value) == 2)) - operator overloading VB: - has optional parameters - allows types within interfaces (although I've never wished I had this in C#) - has a very flexible Select construct (much, much more flexible than 'switch') - has the "When" filter for catch block headers (ok - I'm really stretching now...) And this one is a doubtful benefit of VB: it gives you half a dozen ways to do a lot of things - some regard it as a benefit, while others hate it since it makes it harder to produce standard code in teams. David Anton Tangible Software Solutions www.tangiblesoftwaresolutions.com Home of: Instant C#: VB.NET to C# Converter Instant VB: C# to VB.NET Converter Instant J#: VB.NET to J# Converter
Hi David. Have I bought your program ? Whichever VB->C# converter I bought, it truly rocked !!! David Anton wrote: allows unsafe code (I've yet to meet anyone who does this in C# though) You're talking to one right now. Vital for image processing, although I'm told there's a non-unsafe way, I am yet to see the code to know what it is ( setpixel does NOT count ). David Anton wrote: has the 'as' operator (but VB has it in 2005 - 'TryCast') Trouble is, most VB.NET users prefer to use the older casting operators, the ones that were in VB6. The problem with VB.NET is not lack of features, it's terrible hacks that have been maintained for legacy support ( and which Microsoft initally wanted to remove ). David Anton wrote: VB.NET : has optional parameters Yeah, these would be nice, although I read a rationale against them online which made sense. All about performance. David Anton wrote: And this one is a doubtful benefit of VB: it gives you half a dozen ways to do a lot of things - some regard it as a benefit, while others hate it since it makes it harder to produce standard code in teams. A problem that VB.NET only intensifies. It's my experience, from working on projects that are already in progress, that on average, VB.NET users know a hell of a lot less about good design and coding standards than C# users. It's also my experience that they don't use the new VB.NET features, because they are coding as if it was still VB6. I doubt many of them even know what it is they are asking the compiler to do. Whichever converter I bought, it won me a whole lot of work, because I was able to convert a huge VB.NET program to C#, and I have worked on it ever since. I agree, style is an issue, I hate VB.NET syntax, but there's more to it than that, and the core issue is the sort of people who are using each language, on average. I'm sure there are good VB.NET coders and bad C# coders in the world, but overall, the trend is in the other direction. Christian Graus - Microsoft MVP - C++