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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:00:39+00:00 2026-05-23T14:00:39+00:00

Solution to call non-generic method and pass generic arguments with different generic types? My

  • 0

Solution to call non-generic method and pass generic arguments with different generic types?

My imaginary dream:

void FooBulous(Foo<object>[] fooArray) {  } // Accept any 'Foo<BaseType>'

var fooArray = new Foo<object>[]    // Array of 'Foo<BaseType>'
{
  new Foo<Cat>(),
  new Foo<Dog>(),
};

FooBulous(fooArray); // Pass the 'Foo<BaseType>[]' array

My reality:

void BarBaric(object[] barArray) {  } // Can't constrain to 'Foo<>'

var barArray = new object[]  // Same problem
{
  new Bar<Cat>(),
  new Bar<Dog>(),
};

BarBaric(barArray); // Barbaric! I thought the 'void *ptr' days were over!

In summary:

void Fee(object[] params)      { /* WORKS! But not constrained to 'Foo<Base>' */ }
void Fie(Foo<Cat>[] params)    { /* Does not accept 'Foo<Dog>' */ }
void Foe(Foo<>[] params)       { /* ERROR: 'Type expected' */ }
void Fum(Foo<object>[] params) { /* Cannot convert 'Foo<Cat>' to 'Foo<object>' */ }

Clearly, this can’t be done… Is there a clean alternative?

  • 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-23T14:00:39+00:00Added an answer on May 23, 2026 at 2:00 pm

    The problem is that a Foo<Cat> isn’t a Foo<object>. Suppose Foo looked like this:

    public class Foo<T>
    {
        public void Method(T input)
        {
            ...
        }
    }
    

    Then a Foo<Cat> would always expect a Cat value for the input parameter to Method. But if you could treat Foo<Cat> as a Foo<object> you could do:

    Foo<Cat> catFoo = new Foo<Cat>();
    Foo<object> objectFoo = catFoo;
    objectFoo.Method(new object()); // Eek! Type safety is broken!
    

    Now generic variance is available in .NET 4 (and C# 4) but only for interfaces and delegates, and only for those decorated appropriate with out and in at the point of the type parameter declaration. That may or may not be useful to you.

    Another option is to make Foo<T> derive from an abstract non-generic base class Foo which provides all the members which have nothing to do with T. Then you could write:

    void Foobulous(Foo[] fooArray)
    
    ...
    
    Foobulous(new Foo[] {
        new Foo<object>(),
        new Foo<Cat>(),
        new Foo<Dog>()
    });
    

    and all would be well, so long as Foobulous didn’t need to use any of the methods which relied on T – which it shouldn’t, given that it can take Foo values with different type parameters anyway.

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

Sidebar

Related Questions

I have two WIN32 DLL projects in the solution, main.dll should call a function
Can get work the attribute destroy-method. First, even if I type non-existing method name
Is it possible to use a wild card or call a method to work
Is it possible to call a function defined in a non-specialised template class from
I am trying to find a solution to 'break into non-public methods'. I just
I have a class that should call a visitor method for every member variable.
My requirement is to call WCF web service from ASP.NET code behind and pass
We have a quite common object in our application. In this case, we'll call
Solution -- WorkflowProject   -- Workflow1   -- Workflow2 -- WebProject (WAP)   -- App_Data     -- MyDatabase.vdb3
Solution: I've misinterpreted the example from SQL Books Online. Yes, that below the section

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.