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

  • Home
  • SEARCH
  • 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 7882697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:20:44+00:00 2026-06-03T04:20:44+00:00

If I have a method signature like this; public void Execute(Action<T> action) { …

  • 0

If I have a method signature like this;

public void Execute(Action<T> action) {
    ...
}

but I want to constrain it so that the supplied ‘action’ only comes from the class ‘MyActions’, how can this be achieved in c#?


In order to try and make it clearer;

For example I have a class called MyActions;

public class MyActions {
    public void FirstAction<T>(T item) {
        ...
    }

    public void SecondAction<T>(T item) {
        ...
    }
}

I have the above method and I want it so that the only actions that the method will accept are those from this class.

I do not want it to be possible for anyone to supply an arbitrary action, they must come from the class ‘MyActions’.

Can it be done?

Regards,

Ryan.

  • 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-03T04:20:45+00:00Added an answer on June 3, 2026 at 4:20 am

    One way to make clear your intent that you want to accept only members of MyActions would be something like:

    public void Execute(Func<MyActions, Action<T>> actionSelector)
    

    You could then call it like

    Execute(actions => actions.SomeAction);
    

    Of course, this works only if the methods (or delegate properties) of MyActions are not static.

    And like I said, this makes your intent clear, but doesn’t actually constraint the methods, someone could still call your method like:

    Execute(ignored => otherAction);
    

    In general this seems to me like a weird thing to want.

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

Sidebar

Related Questions

I have a method signature that looks like public void (Foo<T> foo) In this
Imagine that you have a method with the following signature: public void DoSomething(Guid id)
I have a web method with a signature like this: public string[] ToUpper(string[] values)
I have the following method signature: public void MyFunction(Object[,] obj) I create this object:
If I have a method signature like public string myMethod<T>( ... ) How can
I have a method with this signature, and another method that will be the
I have a method with the signature public void setFoo(int newFoo) in a model
Finding myself in the situation where I have a method with this signature void
I have an extension method with the following signature: public static Expression<Func<T, bool>> And<T>(this
In the signature of a method I specify a Func, like so: public void

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.