7 Replies - 6855 Views - Last Post: 03 May 2013 - 08:44 AM

#1 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,675
  • Joined: 24-August 11

Optional Arguments

Posted 30 April 2013 - 02:01 PM

I didn't even know this was a C# language feature until I was researching it during the enterprise-ification of an awful Access application, and found references to it in C#. I've gotten by without it just fine, but now that I know about it I've had several instances where it seemed like a more concise route (for example, avoiding multiple constructor overloads). What do you guys think of this feature? Does it have a place, or would you consider it "dirty" at all? Or do I just feel dirty because it came from the VB world?

Is This A Good Question/Topic? 0
  • +

Replies To: Optional Arguments

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: Optional Arguments

Posted 30 April 2013 - 02:08 PM

MSDN reference
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9098
  • View blog
  • Posts: 34,183
  • Joined: 12-June 08

Re: Optional Arguments

Posted 30 April 2013 - 02:11 PM

Naw.. nothing dirty from it. I use it from time to time as it is needed. Typically I use it as short cut when I have an exiting state I can throw in an optional parameter in a critical method that shoots the processing into a different direction without having to recode a bajillion things.
Was This Post Helpful? 2
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,450
  • Joined: 29-May 08

Re: Optional Arguments

Posted 30 April 2013 - 02:24 PM

It was only recently added to C# (C# 4), to make interfacing with COM component a lot simpler.

This post has been edited by AdamSpeight2008: 30 April 2013 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,675
  • Joined: 24-August 11

Re: Optional Arguments

Posted 30 April 2013 - 02:24 PM

View Postandrewsw, on 30 April 2013 - 04:08 PM, said:



This was one of the references of which I spoke. :)

View Postmodi123_1, on 30 April 2013 - 04:11 PM, said:

Naw.. nothing dirty from it. I use it from time to time as it is needed. Typically I use it as short cut when I have an exiting state I can throw in an optional parameter in a critical method that shoots the processing into a different direction without having to recode a bajillion things.


That's how I was figuring it. I guess it's just one of those things for me where, absent the guidance of a senior developer to keep me picking up things I *shouldn't* do, I wanted to err on the side of caution before I stuck it in production code.

View PostAdamSpeight2008, on 30 April 2013 - 04:24 PM, said:

It was only recently added to C#, to make interfacing with COM component a lot simpler.


I saw that it was finally brought over from the VB world in C# 4.0, which surprised me a bit. I guess it just seemed too easy, so it felt like bad C#...

This post has been edited by h4nnib4l: 30 April 2013 - 02:30 PM

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3553
  • View blog
  • Posts: 11,017
  • Joined: 05-May 12

Re: Optional Arguments

Posted 01 May 2013 - 06:37 AM

If I believe in Inversion of Control, Dependency Injection, and further believe that everything must be passed by constructor parameter injection, then the optional parameters doesn't buy me much because I need to pass in all the parameters anyway.

If I claim that being able to label the parameters passed in provides legibility/documentation because I have a ton a parameters, then that means that I've done something wrong because my class has too many concerns if it has that many parameters.

On the other hand, if I believe that property injection instead of constructor parameter injection is the way to go, then why would I even need optional parameters?

Choices. Choices. Which convictions should I hold on to tightly, and which one are okay to fudge on? And even worse, should those convictions hold even if it's the eleventh hour, your bug that is holding up shipping the product, and you could just do a three line fix with an optional parameter, or churn tens of lines of code?

I will admit that the feature is very very useful for two things. Primarily when dealing the Office object model and not having to have a bajillion parameters to pass in. And secondarily for not having to write a ton of method overloads where each method is just a variation on a call to an uber method. Clarity it adds to the log4net output of not having the noise of those extra layers being logged is very nice to have.
Was This Post Helpful? 0
  • +
  • -

#7 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Optional Arguments

Posted 03 May 2013 - 06:52 AM

I think optional parameters could be used instead of constructor overloading to some extent... For example:

//instead of:
class MyClass
{
   MyClass(string first)
   {//initialization
   }

   MyClass(string first, int second)
   {//initialization
   }
}

//You can have:
internal class MyClass
{
    public MyClass(string first, int? second = null)
    {
        //initialization
    }
}




and so on...
Edit: and it gets even better with more arguments, because you can use argument naming:
class MyClass
{
    public MyClass(string first, int? second = null, int? third = null)
    {
        //initialization
    }
}
//and use it:
var my = new MyClass("something", third: 5);



No need for all possible constructor combinations.

This post has been edited by lucky3: 03 May 2013 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4469
  • View blog
  • Posts: 7,783
  • Joined: 08-June 10

Re: Optional Arguments

Posted 03 May 2013 - 08:44 AM

Quote

Clarity it adds to the log4net output of not having the noise of those extra layers being logged is very nice to have.


That's the main place we've used it, in our logging wrapper. We made the severity parameter optional, since we mostly just log informational messages.

Personally, I like the feature. By adding default values for some parameters, you effectively create overloads without having to write additional methods. I think that any feature that results in me writing less code to achieve the same outcome is killer.

Quote

It was only recently added to C# (C# 4), to make interfacing with COM component a lot simpler.


Interesting side note, it's a feature added into the C# 4 compiler. I didn't realize this until I switched a project's output to .NET 3.5 and my code still compiled. It's an interesting distinction. Extension methods are the same kind of trick; not a framework thing, a compiler thing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1