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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:34:13+00:00 2026-05-14T23:34:13+00:00

In C# I am trying to write code where I would be creating a

  • 0

In C# I am trying to write code where I would be creating a Func delegate which is in itself generic. For example the following (non-Generic) delegate is returning an arbitrary string:

Func<string> getString = () => "Hello!";

I on the other hand want to create a generic which acts similarly to generic methods. For example if I want a generic Func to return default(T) for a type T. I would imagine that I write code as follows:

Func<T><T> getDefaultObject = <T>() => default(T);

Then I would use it as

getDefaultObject<string>() which would return null and if I were to write getDefaultObject<int>() would return 0.

This question is not merely an academic excercise. I have found numerous places where I could have used this but I cannot get the syntax right. Is this possible? Are there any libraries which provide this sort of functionality?

  • 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-14T23:34:13+00:00Added an answer on May 14, 2026 at 11:34 pm

    Though one might find practical workarounds like Stephen Cleary’s

    Func<T> CreateGetDefaultObject<T>() { return () => default(T); }
    

    where you can specify the generics directly, this is a quite interesting problem from a theoretical point that cannot be solved by C#’s current type system.


    A type which, as you call it, is in itself generic, is referred to as a higher-rank type.

    Consider the following example (pseudo-C#):

    Tuple<int[], string[]> Test(Func<?> f) {
        return (f(1), f("Hello"));
    } 
    

    In your proposed system, a call could look like that:

    Test(x => new[] { x }); // Returns ({ 1 }, { "Hello" })
    

    But the question is: How do we type the function Test and it’s argument f?
    Apparently, f maps every type T to an array T[] of this type. So maybe?

    Tuple<int[], string[]> Test<T>(Func<T, T[]> f) {
        return (f(1), f("Hello"));
    } 
    

    But this doesn’t work. We can’t parameterize Test with any particular T, since f should can be applied to all types T. At this point, C#’s type system can’t go further.

    What we needed was a notation like

    Tuple<int[], string[]> Test(forall T : Func<T, T[]> f) {
        return (f(1), f("Hello"));
    } 
    

    In your case, you could type

    forall T : Func<T> getDefaultValue = ...
    

    The only language I know that supports this kind of generics is Haskell:

    test :: (forall t . t -> [t]) -> ([Int], [String])
    test f = (f 1, f "hello")
    

    See this Haskellwiki entry on polymorphism about this forall notation.

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

Sidebar

Ask A Question

Stats

  • Questions 434k
  • Answers 434k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer WordPress implements a number of APIs to allow this: http://codex.wordpress.org/XML-RPC_Support May 15, 2026 at 3:21 pm
  • Editorial Team
    Editorial Team added an answer One way would be to create a new working branch… May 15, 2026 at 3:21 pm
  • Editorial Team
    Editorial Team added an answer Pick your particular "NoSQL" database first -- or at least… May 15, 2026 at 3:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.