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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:59:35+00:00 2026-06-12T18:59:35+00:00

This is a hard question about language design, patterns and semantics. Please, don’t down-vote

  • 0

This is a hard question about language design, patterns and semantics. Please, don’t down-vote just because you don’t see the practical value.

First, let’s think about functions and their parameters. Then we’ll look at the analogies between functions with their parameters/arguments and generic classes/functions with their type-parameters/type-arguments.

Functions are blocks of code with some unspecified values called “parameters“.
You supply the arguments and receive the result.

Generic classes are classes with some unspecified “type-parameters“.
You supply the type-arguments and then you can work with the class – call the constructor or invoke static methods.

Generic functions in non-generic classes are functions with some unspecified “type-parameters” and some unspecified “value-parameters“.
You supply the type-arguments and value-arguments to receive result.

Delegates are pointers to specific functions. When you create delegate you don’t specify the function arguments, but supply them later.

The problem is that .Net doesn’t have equivalent of Delegates for generic functions with unspecified generic type-parameters. You cannot supply type-values for the type-parameters later.
We can imagine delegates that have not only free value parameters, but also free type-parameters.

static class SomeClass {
    //generic function
    public static T GetValue<T>() {
        return default(T);
    }
}

//creating delegate to generic function or method group
Func{TFree}<TFree> valueFactory = SomeClass.GetValue;

//creating delegate to anonymous generic function
Func{TFree}<int, List<TFree>> listFactory = {TFree}(int capacity) => new List<TFree>(capacity);

Below is the [pseudo]code for a program that I want to write in C#. I want to know how one can achieve the similar behavior in a correct C# program.

How can we emulate delegates with free generic type-parameters in C#?

How can we pass the reference/link to generic function[s] with yet-unknown generic parameters through the non-generic code?

public static class Factory { //Everything compiles fine here
    public delegate ICollection<T> FactoryDelegate<T>(IEnumerable<T> values);

    public static ICollection<T> CreateList<T>(IEnumerable<T> values) {
        return new List<T>(values);
    }

    public static ICollection<T> CreateSet<T>(IEnumerable<T> values) {
        return new HashSet<T>(values);
    }
}

public class Worker { //non-generic class
    Func{TFree}<FactoryDelegate<TFree>> _factory; //TFree is a "free" generic type paramenter

    public Worker(Func{TFree}<FactoryDelegate<TFree>> factory) {
        _factory = factory;
    }

    public ICollection<T> DoWork<T>(IEnumerable<T> values) { //generic method
        return _factory{T}(values); //supplying T as the argument for type parameter TFree
    }
}

public static class Program {
    public static void Main() {
        string[] values1 = new string[] { "a", "b", "c" };
        int[] values2 = new int[] { 1, 2, 2, 2 };

        Worker listWorker = new Worker(Factory.CreateList); //passing reference to generic function
        Worker setWorker = new Worker(Factory.CreateSet); //passing reference to generic function

        ICollection<string> result1 = listWorker.DoWork(values1);
        ICollection<int> result2 = listWorker.DoWork(values2); //.Count == 4
        ICollection<int> result3 = setWorker.DoWork(values2); //.Count == 2
    }
}

See how we pass the references to generic functions (Factory.CreateList and Factory.CreateSet) to the Worker class constructor without specifying the type arguments? Type arguments are supplied later when the generic DoWork function is called with concrete-typed arrays. DoWork uses the type-arguments to select the correct function, passes value-arguments to it and returns the received value.

Final solution: Emulating delegates with free generic type parameters 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-12T18:59:37+00:00Added an answer on June 12, 2026 at 6:59 pm

    I think the way you emulate this in the language is by not using delegates but interfaces. A non-generic interface can contain a generic method, so you can get most of the behavior of delegates with open type arguments.

    Here is your example re-worked into a valid C# program (Note that it still requires the Factory class you defined):

    public interface IWorker
    {
        ICollection<T> DoWork<T>(IEnumerable<T> values);
    }
    
    public class ListCreationWorker : IWorker
    {
        public ICollection<T> DoWork<T>(IEnumerable<T> values)
        {
            return Factory.CreateList<T>(values);
        }
    }
    
    public class SetCreationWorker : IWorker
    {
        public ICollection<T> DoWork<T>(IEnumerable<T> values)
        {
            return Factory.CreateSet<T>(values);  
        }
    }
    
    public static class Program {
        public static void Main(string[] args) {
            string[] values1 = new string[] { "a", "b", "c" };
            int[] values2 = new int[] { 1, 2, 2, 2 };
    
            IWorker listWorker = new ListCreationWorker();
            IWorker setWorker = new SetCreationWorker();
    
            ICollection<string> result1 = listWorker.DoWork(values1);
            ICollection<int> result2 = listWorker.DoWork(values2); //.Count == 4
            ICollection<int> result3 = setWorker.DoWork(values2); //.Count == 2
        }
    }
    
    public static class Factory
    {
        public static ICollection<T> CreateSet<T>(IEnumerable<T> values)
        {
            return new HashSet<T>(values);
        }
    
        public static ICollection<T> CreateList<T>(IEnumerable<T> values)
        {
            return new List<T>(values);
        }
    }
    

    You still get the important feature of separating the decision of which method to call from the execution of said method.

    One thing that you cannot do, however, is store any state in the the IWorker implementations in a generic fashion. I’m not sure how that could be useful because the DoWork method could be called with different type arguments every time.

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

Sidebar

Related Questions

please help, this is my 4th question about this, I trying so hard I
This question is more UI/Design-ish than hard-core programming is. Background: I've been coding in
A question about c++ design and efficiency... Imagine this code - Database db; class
In this answer to a question about the definitions of NP, NP-hard, and NP-complete,
This is mostly a theoretical question I'm just very curious about. (I'm not trying
This question is hard to describe succinctly, so bear with me. Currently I have
This question is hard to phrase, so I'm going to have to use some
This is a very hard to explain question and I hope my code extract
This question's solution makes hard-coded transformation of a sample XML tree into a flat
This should be an easy question - but I'm having a hard time figuring

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.