Click here to Skip to main content
15,122,406 members
Articles / Programming Languages / C#
Tip/Trick
Posted 13 Sep 2016

Tagged as

Stats

10K views
2 bookmarked

Optional Parameters that Aren't (optional, that is)

Rate me:
Please Sign up or sign in to vote.
4.71/5 (12 votes)
13 Sep 2016CPOL1 min read
This came as a mild surprise

Heads Up

I have an existing class that has a method called UpdateStatus that updates a list of stringsdisplayed in the UI. I created another class that I wanted to get status messages from, but I didn't want to change the original class to expose it's UpdateStatus method.

My solution was to pass the method as an Action to the constructor of the new class.

The method prototype (in the original class) looks like this - notice the default parameter:

C#
public class MyOldClass
{
    public void UpdateStatus(string msg, bool isError=false)
    {...}
    
    // this method instantiates MyNewClass
    private void SomeOtherMethod()
    {
        MyNewClass myClass = new MyNewClass(this.UpdateStatus);
    }
}

The class I was writing looked like this:

C#
public class MyNewClass
{
    public Action<string, bool> UpdateStatus { get; private set; }
    
    public MyClass(Action<string, bool> updateStatus)
    {
        this.UpdateStatus = updateStatus;
    }
}

When I tried to call the action delegate within the new class, like so:

this.UpdateStatus("some text");

I found out that any optional parameters that might be specified in the actual method's prototype revert to *required* parameters, thus forcing me to specify a value for the isError parameter:

this.UpdateStatus("some text", false);

In case you're interested, you cannot define the Action property (in the new class) as having a default parameter, either.

This is just a little heads up for anyone else doing the same thing.

Additional Info

I could have used a delegate:

C#
public delegate void UpdateStatusDelegate(string msg, bool isError=false);
public UpdateStatusDelegate UpdateStatus { get; set; }

public MyNewClass(UpdateStatusDelegate updateStatus)
{
    this.UpdateStatus = updateStatus;
}

...allowing me to keep the default parameter paradigm, but that's more a matter of style than propriety in this case. If I had wanted to use ref or out parameters, a delegate would have been required since an Action also doesn't support those types of parameters.

History

  • 14 SEP 2016 - Made the text a little clearer as to what I was doing and what was happening.
     
  • 13 SEP 2016 - Original publication.
     

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

#realJSOP
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
QuestionUse an Action<string> Pin
Dennis_E15-Sep-16 23:38
professionalDennis_E15-Sep-16 23:38 
QuestionBut of course not! Pin
Galatei14-Sep-16 7:45
MemberGalatei14-Sep-16 7:45 
QuestionWhy not use delegate? Pin
johannesnestler14-Sep-16 2:24
Memberjohannesnestler14-Sep-16 2:24 
AnswerRe: Why not use delegate? Pin
#realJSOP14-Sep-16 3:09
mva#realJSOP14-Sep-16 3:09 
GeneralRe: Why not use delegate? Pin
johannesnestler14-Sep-16 4:06
Memberjohannesnestler14-Sep-16 4:06 
GeneralRe: Why not use delegate? Pin
#realJSOP14-Sep-16 4:41
mva#realJSOP14-Sep-16 4:41 
QuestionNot so much optional as a hint to the compiler Pin
John Brett13-Sep-16 22:33
MemberJohn Brett13-Sep-16 22:33 
AnswerRe: Not so much optional as a hint to the compiler Pin
johannesnestler14-Sep-16 2:28
Memberjohannesnestler14-Sep-16 2:28 
AnswerRe: Not so much optional as a hint to the compiler Pin
#realJSOP14-Sep-16 3:10
mva#realJSOP14-Sep-16 3:10 
GeneralRe: Not so much optional as a hint to the compiler Pin
John Brett14-Sep-16 4:38
MemberJohn Brett14-Sep-16 4:38 
QuestionNot very clear... Pin
Philippe Mori13-Sep-16 18:31
MemberPhilippe Mori13-Sep-16 18:31 
You have both a action and a method UpdateStatus... Thus it is not clear what you want to demonstrate.

I would recommand you to update code so that it is clear where UpdateStatus method (first code block) is declared.
Philippe Mori

AnswerRe: Not very clear... Pin
#realJSOP14-Sep-16 3:11
mva#realJSOP14-Sep-16 3:11 
GeneralRe: Not very clear... Pin
Philippe Mori14-Sep-16 7:09
MemberPhilippe Mori14-Sep-16 7:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.