Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8642077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:41:11+00:00 2026-06-12T11:41:11+00:00

Say, we have ClassA with method Foo containing an optional parameter. So, we can

  • 0

Say, we have ClassA with method Foo containing an optional parameter. So, we can use it as shown in method DoFoo.

public class ClassA
{
    public ClassA() { }

    public void Foo(bool flag = true)
    {
    }

    public void DoFoo()
    {
        Foo(); // == Foo(true);
    }
}

Once I needed to pass it to another class ClassB. First I tried to pass it as Action, but the signature surely didn’t match. Then I passed it as Action<string>, the signature matched, but the parameter in ClassB was no longer optional. But I did wanted to have it optional and came to an idea to declare a delegate. So, it worked.

public delegate void FooMethod(bool flag = true);

public class ClassB
{
    Action<bool> Foo1;
    FooMethod Foo2;

    public ClassB(Action<bool> _Foo1, FooMethod _Foo2)
    {
        Foo1 = _Foo1;
        Foo2 = _Foo2;
    }

    public void DoFoo()
    {
        Foo1(true);
        Foo2(); // == Foo2(true);
    }

So, the question is: can I somehow pass a method with an optional parameter as an argument without explicitly declaring a delegate and keep the optional quality of its parameters?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:41:12+00:00Added an answer on June 12, 2026 at 11:41 am

    So, the question is: can I somehow pass a method with an optional parameter as an argument without explicitly declaring a delegate and keep the optional quality of its parameters?

    No. The “optionality” is part of the signature of the method, which the compiler needs to know at at compile time to provide the default value. If you’re using a delegate type that doesn’t have the optional parameter, what is the compiler meant to do when you try to call it without enough arguments?

    The simplest approach is probably to wrap it:

    CallMethod(() => Foo()); // Compiler will use default within the lambda.
    ...
    public void Foo(bool x = true) { ... }
    
    public void CallMethod(Action action) { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have base class A. it has method void foo(A* a); It also
Let's say we have some Foo class that contains public void setOnSomethingListener(OnSomethingListener listener); We
So in java, say you have a non-static method 'bar()' in an class 'Foo'.
Let's say I have a static class with a static method. Multiple threads can
Silly question, but say you have class Foo: class Foo { public: typedef boost::shared_ptr<Foo>
Lets say we have a simple class public class Foo { public string FooName;
Say you have a C++ class like: class Foo { public: virtual ~Foo() {}
Say I have CFooer and it implements method void foo(). I then have ISomething
Lets say I have a method that looks like this: public static String[] parseFoo(Foo
Let say I have a class Foo like this public class Foo { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.