Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C#
Tip/Trick

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 13.2K   2   13
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)


Written By
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 22:38
professionalDennis_E15-Sep-16 22:38 
QuestionBut of course not! Pin
Galatei14-Sep-16 6:45
Galatei14-Sep-16 6:45 
QuestionWhy not use delegate? Pin
johannesnestler14-Sep-16 1:24
johannesnestler14-Sep-16 1:24 
AnswerRe: Why not use delegate? Pin
#realJSOP14-Sep-16 2:09
mve#realJSOP14-Sep-16 2:09 
GeneralRe: Why not use delegate? Pin
johannesnestler14-Sep-16 3:06
johannesnestler14-Sep-16 3:06 
GeneralRe: Why not use delegate? Pin
#realJSOP14-Sep-16 3:41
mve#realJSOP14-Sep-16 3:41 
QuestionNot so much optional as a hint to the compiler Pin
John Brett13-Sep-16 21:33
John Brett13-Sep-16 21:33 
AnswerRe: Not so much optional as a hint to the compiler Pin
johannesnestler14-Sep-16 1:28
johannesnestler14-Sep-16 1:28 
What? I'm just another commenter, but I don't get what you want to tell us here? see my comment - in the end the problem is that he does not use his mehtod's signature as a specifc delegate-type (then optional parameters work of course), but "converts" it to a predefinded Action delegate(which doesn't support optional parameters...)

AnswerRe: Not so much optional as a hint to the compiler Pin
#realJSOP14-Sep-16 2:10
mve#realJSOP14-Sep-16 2:10 
GeneralRe: Not so much optional as a hint to the compiler Pin
John Brett14-Sep-16 3:38
John Brett14-Sep-16 3:38 
QuestionNot very clear... Pin
Philippe Mori13-Sep-16 17:31
Philippe Mori13-Sep-16 17:31 
AnswerRe: Not very clear... Pin
#realJSOP14-Sep-16 2:11
mve#realJSOP14-Sep-16 2:11 
GeneralRe: Not very clear... Pin
Philippe Mori14-Sep-16 6:09
Philippe Mori14-Sep-16 6: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.