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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:14:01+00:00 2026-05-18T20:14:01+00:00

I would like to write an extension method for a generic type, with additional

  • 0

I would like to write an extension method for a generic type, with additional generic type parameters. I have working code already, but don’t like the result, because the user is required to re-enter the generic type parameters of the existing type.

I have a concrete example, but keep in mind that this is a general problem. I appreciate feedback on the particular problem at hand, but I am looking for general solutions.

For a class of type Action<T>, add an extension method with a signature similar to this:

Func<T, TResult> ToFunc<TResult>();

Here’s my working code:

public static class DelegateExtensions
{
  public static Func<T, TResult> ToFunc<T, TResult>(this Action<T> action)
  {
    return arg => { action(arg); return default(TResult); };
  }
}

But the usage stinks:

public void TakesAFunc(Func<int, float> someFunc) { /* ... */ }
// ...
Action<int> someIntAction = /* ... */;
TakesAFunc(someIntAction.ToFunc<int, float>());

In this example, the int generic parameter is the only value that is acceptable, so it causes unnecessary code duplication.

Type inference does not work here. I think this is because you can’t infer a generic parameter via return type.

This code would solve the problem, but unfortunately does not seem to work:

public static class DelegateExtensions<T>
{
  public static Func<T, TResult> ToFunc<TResult>(this Action<T> action)
  {
    return arg => { action(arg); return default(TResult); };
  }
}

Usage would be exactly like you’d expect:

public void TakesAFunc(Func<int, float> someFunc) { /* ... */ }
// ...
Action<int> someIntAction = /* ... */;
TakesAFunc(someIntAction.ToFunc<float>());

I noticed that System.Linq.Queryable works the way my first chunk of code does, though it usually doesn’t need extra type parameters, so type inference works.

Is there any known trick to get around requiring these duplicate generic type parameters? One thought that comes to mind would be code generators or macros of some sort, but I can’t think of how I’d do it cleanly.

  • 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-18T20:14:02+00:00Added an answer on May 18, 2026 at 8:14 pm

    One common trick for this sort of thing is to pass a dummy value of the desired type just for type inference, which is required in certain scenarios involving anonymous types.

    public static class DelegateExtensions
    {
      public static Func<T, TResult> 
      ToFunc<T, TResult>(this Action<T> action, TResult ignored)
      {
        return arg => { action(arg); return default(TResult); };
      }
    }
    

    How good of a trick this is is up to personal opinion, but this would confuse me every time I called it and every time I came back to a place that I called it. You could also change the semantics of the method to make the second parameter be the return value, allowing the caller to decide if they want the default value or not.

    As to your second example, I’m pretty sure extension methods are not allowed on generic types, but I don’t have a link handy

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

Sidebar

Related Questions

I would like to write an extension method for a generic type, with additional
I'm trying to write a generic extension method that let's me do this: this.startDate
I would like to write a small program in C# which goes through my
I would like to write some data to a file in Ruby. What is
I would like to write a plug-in that will allow a custom written CRM
I would like to write my own OS, and would like to temporarily jump
I would like to write some scripts in python that do some automated changes
I would like to write a program that will identify a machine( for licensing
I would like to write a VBScript to change the default printer, based on
I would like to write a utility that will provide me with a relatively

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.