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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:00:02+00:00 2026-06-18T23:00:02+00:00

How to pass a List with different inherited classes to the function takes the

  • 0

How to pass a List with different inherited classes to the function takes the parent interface List?
The compiler generates an error.

    public interface ControlPoint
    {
    }

    public class Knot:  ControlPoint
    {
    }

    public class Node:  ControlPoint
    {
    }

    class Spline
    {
         List<Knot> knots=new List<Knot>();
         List<Node> nodes=new List<Node>();

         void Calculate(List<ControlPoint> points) {} //<------- Compiler Error

        void Test()
        {
           Calculate(knots);
           Calculate(nodes);
        }

    }
  • 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-18T23:00:04+00:00Added an answer on June 18, 2026 at 11:00 pm

    Two options:

    1) If you’re using C# 4 and .NET 4+, and you only need to iterate over the points in Calculate, just change the signature to:

        void Calculate(IEnumerable<ControlPoint> points)
    

    This will use generic covariance.

    2) You could make the method generic:

        void Calculate<T>(List<T> points) where T : ControlPoint
    

    I would prefer the first in general – and even if you’re not using .NET 4 / C# 4, if you can change Calculate to use IEnumerable<T> instead of List<T>, that would be cleaner – so you could mix the two and have a generic method taking IEnumerable<T>.

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

Sidebar

Related Questions

I want to pass a property list of a class to a function. with
I want to pass different List collection to TypeToken class in GSON. Here is
If I pass an empty list into a JPA query, I get an error.
I have a List to pass to my view in ViewBag: public ActionResult ContactUs()
I have a SalesOrder class which is inherited by several different types of sales
Is it possible to pass the __ contains __ function in a list more
Basically I want to override some function in the flex/actionscript list class which creates
In a function I pass a list of related values and loop over them
I have this method public List<List<Stat>> ConvertReportCases (ArrayList<ArrayList<Stat>> stats) It is an interface implementation
How would I pass a list of values into a function, where the values

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.