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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:48:14+00:00 2026-05-26T08:48:14+00:00

If I would like to write a method that takes a variable number of

  • 0

If I would like to write a method that takes a variable number of “TDerived” where TDerived is any subclass of a class “Base”, is there any way to do this?

The following code only works with a single specific specified subclass:

void doStuff<TDerived>(params TDerived[] args) where TDerived : Base
{
    //stuff
}

ie if I have

class Super { }
class Sub0 : Super { }
class Sub1 : Super { }

then I cannot do

Sub0 s0 = new Sub0();
Sub1 s1 = new Sub1();
doStuff(s0, s1);

since I get “best overloaded match… has some invalid arguments”.

Regardless of how the compiler handles the type constraints and variadic functions, this seems (as far as I can tell) completely type-safe. I know I could cast, but if this is type safe why not allow it?

EDIT:

Perhaps a more convincing example:

void doStuff<TDerived>(params SomeReadOnlyCollection<TDerived>[] args) where TDerived : Base
{
    foreach(var list in args)
    {
        foreach(TDerived thing in list)
        {
            //stuff
        }
    }
}
  • 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-26T08:48:15+00:00Added an answer on May 26, 2026 at 8:48 am

    TDerived needs to be able to resolve to a single type. In your example, the only type it could resolve to would be Super, but the compiler is not going to make that leap. You can make that leap for the compiler.

    doStuff(new Super[] { s0, s1 });
    doStuff<Super>(s0, s1);
    

    Regarding your update, consider (instead of a generic method) defining a method accepting IEnumerable<ISuper>, which will support derived types because IEnumerable<T> is covariant (as of .NET 4). IEnumerable<T> is also inherently readonly and forward-only, perfect if you have a foreach loop. Full working example:

    class Program
    {
        static void Main()
        {
            var sub0s = new Sub0[] { new Sub0() };
            var sub1s = new List<Sub1> { new Sub1() };
            doStuff(sub0s, sub1s);
        }
    
        static void doStuff(params IEnumerable<ISuper>[] args)
        {
            foreach (var sequence in args)
            {
                foreach (var obj in sequence)
                {
                    Console.WriteLine(obj.GetType());
                    // you have the ability to invoke any method or access 
                    // any property defined on ISuper
                }
            }
        } 
    }
    
    interface ISuper { }
    class Super : ISuper { }
    class Sub0 : Super { }
    class Sub1 : Super { }  
    

    IEnumerable<T> is implemented by BCL collections since .NET 2.0, including T[], List<T>, ReadOnlyCollection<T>, HashSet<T>, etc.

    • 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 a method in ruby that takes a class with
I would like to write a cast method in Eiffel which takes 'the type
I would like to write a plug-in that will allow a custom written CRM
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 utility that will provide me with a relatively
I would like to write an object generator for a templated RAII class --
I need to write a unit test for a method that takes a stream
I would like to write an image labeling tool using PyQT4: load a number
I would like to write a small program in C# which goes through my

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.