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 9101653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:11:11+00:00 2026-06-17T01:11:11+00:00

Edit: whilst back 2013 Jon Skeet’s accepted answer was 100% correct. For anyone coming

  • 0

Edit: whilst back 2013 Jon Skeet’s accepted answer was 100% correct. For anyone coming across this question now (2023) I would direct you to check Sergey.quixoticaxis.Ivanov’s answer below as from C#12 this exact feature is implemented.


Is there a way to use optional arguments (default parameters) with lambda expressions in c#? I have read through the documentation but can find nothing to say one way or the other.

To illustrate, I can define simple method that uses an optional argument to supply a default value like so:

void MyMethod(string arg = "default-value")
{ 
    Console.WriteLine(arg);
}

What I want to know is if I am able to do the same thing using a lambda expression.

// gives a syntax error
Action<string> MyMethod = (arg = "default") => Console.WriteLine(arg);

I can work in an optional parameter with a default value using a delegate, but this seems a bit clumsy.

delegate void MyDelegate(string arg = "default");
MyDelegate MyMethod = arg => Console.WriteLine(arg);

Alternatively I could check the parameter in the lambda body, something like…

 Action<string> MyMethod = (arg) => Console.WriteLine(string.IsNullOrEmpty(arg) ?
                                    "default" :
                                    arg);

But again this seems a bit clumsy.

Is it possible to use optional parameters to set a default value in a lambda expression in c#?

  • 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-17T01:11:12+00:00Added an answer on June 17, 2026 at 1:11 am

    No. The caller (the code invoking the delegate) doesn’t “see” the lambda expression, so it doesn’t make sense to specify the default parameter there. All the caller sees is the delegate. In your case, for example, the calling code only knows about Action<string> – how is the compiler meant to know to supply the default value that’s specified by the lambda expression?

    As an example of how things get tricky, imagine if this were viable. Then consider this code:

    Action<string> action;
    if (DateTime.Today.Day > 10)
    {
        action = (string arg = "boo") => Console.WriteLine(arg); 
    }
    else
    {
        action = (string arg = "hiss") => Console.WriteLine(arg);
    }
    action(); // What would the compiler do here?
    

    Bear in mind that the argument is provided by the compiler at the call site – so what should it do with the final line?

    It’s a bit like having an interface and an implementation – if you have a default parameter on an interface, that’s fine; if you only have it on the implementation, then only callers who know the specific implementation will see it. In the case of lambda expressions, there’s really no visible implementation for the caller to use: there’s just the delegate signature.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
EDIT: Perhaps I am approaching this wrong - does anyone know any way of
Whilst trying to pivot a sql table I came across this post Here .
EDIT: See my answer below--> I am wanting to have a view that when
Edit : Note that, as Daniel and latkin noted in an answer and a
EDIT : It turned out that this can only be done through an external
EDIT: I have no updated the question, whilst doing so I realized the scope
I was messing around with std::ostringstream whilst looking at this question: sprintf in c++?
EDIT: I was an idiot. I simply had an image that was vertically long,
Edit (updated question) I have a simple C program: // it is not important

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.