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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:29:52+00:00 2026-05-21T17:29:52+00:00

I have a class that the user can pass an Action into (or not).

  • 0

I have a class that the user can pass an Action into (or not).

public class FooClass<T> : BaseClass<T>
{
    public FooClass()
        : this((o) => ()) //This doesn't work...
    {
    }

    public FooClass(Action<T> myAction)
        : base(myAction)
    {
    }
}

Basically, I can’t pass null into my base class for the Action. But, at the same time I don’t want to force my user to pass in an Action. Instead, I want to be able to create a “do-nothing” action on the fly.

  • 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-05-21T17:29:53+00:00Added an answer on May 21, 2026 at 5:29 pm

    You want to say

    this(t => { })
    

    Think of it like this. You need t => anonymous-expression-body. In this case, anonymous-expression-body is an expression or a block. You can’t have an empty expression so you can’t indicate an empty method body with an expression. Therefore, you have to use a block in which case you can say { } to indicate the a block has an empty statement-list and is therefore the empty body.

    For details, see the grammar specification, appendix B.

    And here’s another way to think of it, which is a way that you could use to discover this for yourself. An Action<T> is a method that takes in a T and returns void. You can define an Action<T> via an non-anonymous method or via an anonymous method. You are trying to figure out how to do it using an anonymous method (or rather, a very special anonymous method, namely a lambda expression). If you wanted to do this via a non-anonymous method you would say

    private void MyAction<T>(T t) { }
    

    and then you could say

    this(MyAction)
    

    which uses the concept of a method group. But now you want to translate this to a lambda expression. So, let’s just take that method body and make it a lambda expression. Therefore, we throw away the private void MyAction<T>(T t) and replace it with t => and copy verbatim the method body { }.

    this(t => { })
    

    Boom.

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

Sidebar

Related Questions

I have my custom class User : class User { public String name; public
I have a UserPass class which has 2 properties: User user; String password User
I have this custom primary key in a model: class Personal(models.Model): name = models.CharField(max_length=20,primary_key=True)
I have searched the web for a way to do this, but perhaps I'm
I'm trying to create a Django app where the user can make a list
I have the following classes: using System; using System.Windows.Forms; namespace FastEyeControl { public partial
[Preamble: Whereas I realize there may be simpler ways to do this (i.e., just
I have authlogic working fine but now have the need to let administrator users
i have just started programming for android and i have a little question i
So I'm really new to the world of graphics and for an app I'm

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.