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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:26:57+00:00 2026-05-16T11:26:57+00:00

I’m working on a generic delegate function and declaring a return type of type

  • 0

I’m working on a generic delegate function and declaring a return type of type List.

public static List<T> PerformOperationOnGenericArray<T>(IList<T> myList,
    FunctionForGenericArray<T> operation)

Am I able to use a generic return type instead of List that also specifies a generic type, i.e. S

public static S<T> PerformOperationOnGenericArray<T, S>(IList<T> myList, 
    FunctionForGenericArray<T> operation)

I’m guessing that this is not possible but I cannot see the reason as to why not. The compiler surely knows the type when I specify:

PerformOperationOnGenericArray<int, List<string>>(myInts, i => i.Equals(12));

Is this perhaps a situation whereby I need to look at using dynamic types?

  • 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-16T11:26:58+00:00Added an answer on May 16, 2026 at 11:26 am

    You can’t use an open generic type as a generic type parameter, however, you can use a specific type S<T> as a parameter:

    public static R PerformOperationOnGenericArray<T,R>
               (IList<T> myList, FunctionForGenericArray<T> operation) 
        where R : S<T>
    

    In your case, if the type S is a fully defined (closed type) at compiled time, you don’t even need to do that:

    public static S<T> PerformOperationOnGenericArray<T>
               (IList<T> myList, FunctionForGenericArray<T> operation) 
    

    should be sufficient.

    You only need to use the first form if S itself will vary. So what do I mean by this. Let’s look at an example. If you have:

    class Foo<T> { }
    

    you can write:

    public static Foo<T> PerformOperationOnGenericArray<T>
               (IList<T> myList, FunctionForGenericArray<T> operation)
    

    But, if you have some group of related generic types:

    class Foo<T> { }
    class Bar<T> : Foo<T> { }
    class Baz<T> : Foo<T> { }
    

    and you want to allow the caller to specify which one to use, then you need to make the return type part of the generic signature of the method:

    public static R PerformOperationOnGenericArray<T,R>
               (IList<T> myList, FunctionForGenericArray<T> operation) 
        where R : Foo<T>
    

    where callers will now need to specify the type of R explicitly:

    PerformOperationOnGenericArray<T,Bar<T>>( ... )
    

    If you want to allow any generic type that accepts a single parameter to be allowed, there you are out of luck. The type system doesn’t provide a way to express the restriction:

    allow any generic type that allows a single parameter, and enforce that parameter to be a T

    The best you can do is to define a well known interface that all known types conform to (like IEnumerable<T>, or one you craft yourself), and use that as part of the generic constraint:

    public static R PerformOperationOnGenericArray<T,R>
               (IList<T> myList, FunctionForGenericArray<T> operation) 
        where R : IEnumerable<T>
    

    However, in this case, the types supplied must all implement this interface. If you’re looking for the former (where you can specify any type that just has to match on the number of type parameters) you’re looking for generic duck typing(1) – which C# doesn’t support.

    An interesting side note on this. Creating a generic method whose types cannot be inferred by the compiler solely from the formal parameters of the method is something to try to avoid. When the parameters cannot be inferred, the caller is forced to specify all of the type parameters – which leads to confusing and verbose code. While sometimes these situations do come up, you’re better off trying to avoid them, where possible.

    (1) – C# does support duck typing in anonymous types within a single assembly if the types match in the order, types, and names of their members – upon which the compiler will assume that they are the same type.

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

Sidebar

Ask A Question

Stats

  • Questions 502k
  • Answers 502k
  • 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 td.threecol{ width:28%; padding-right:20px; padding-left:20px; font-size:11px; color:#444; padding-top:20px; border-right:1px solid #ccc;… May 16, 2026 at 2:44 pm
  • Editorial Team
    Editorial Team added an answer This is fairly simple. First, you'll need to obtain height… May 16, 2026 at 2:44 pm
  • Editorial Team
    Editorial Team added an answer Same answer as you previous question. The retail automation interface… May 16, 2026 at 2:44 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

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.