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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:13:54+00:00 2026-05-27T19:13:54+00:00

Say I have this method public void test(IList<AvaliableFeaturesVm> vm) { } I have this

  • 0

Say I have this method

 public void test(IList<AvaliableFeaturesVm> vm)
        {

        }

I have this class that inherits my base class

public class TravelFeaturesVm : AvaliableFeaturesVm
    {

    }

how do I pass it into method “test”. If test only accepted one AvaliableFeatureVm object it would let me pass in a TravelFeaturesVm object but since they are in a collection it does not let me.

  • 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-27T19:13:55+00:00Added an answer on May 27, 2026 at 7:13 pm

    As of C# 4, you can do this if you change the signature of the first call to:

    public void test(IEnumerable<AvaliableFeaturesVm> vm)
    {
    }
    

    This is due to generic covariance which was introduced in C# 4. IEnumerable<T> is covariant in T in C# 4, but IList<T> is invariant, so it won’t as-is. If you’re not using C# 4, this won’t work anyway.

    This is assuming that all your method needs to do is iterate over the collection. If it needs to modify the collection, then it wouldn’t be safe to pass in a list of the subtype anyway – because the method could try to add either an instance of the base class, or of a different subtype.

    Another alternative is to make the method itself generic:

    public void test<T>(IList<T> vm) where T : AvaliableFeaturesVm
    

    Of course, this has its own limitations – in particular, again you won’t be able to write:

    vm.Add(new AvailableFeaturesVm());
    

    … although you can do:

    AvailableFeaturesVm first = vm[0];
    

    as the constraint ensures that there’s an appropriate conversion from T to AvailableFeaturesVm.

    For more on generic variance, see Eric Lippert’s blog posts.

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

Sidebar

Related Questions

Let's say I have a method public void Foo(string bar) that the caller should
Let's say I for example have this class that generates Fibonacci numbers: public class
Lets say I have an interface passed to my method: public void AlphaToChar(iList _blah)
Say I have this simple method: public IEnumerable<uint> GetNumbers() { uint n = 0;
Lets say have this immutable record type: public class Record { public Record(int x,
Let's say I have a C# method public void CheckXYZ(int xyz) { // do
say I have a method I wish to test: public Cars[] GetCars(); I want
This is with C# and .net 3.5 Let's say I have the following method:
Say I have this simple form: class ContactForm(forms.Form): first_name = forms.CharField(required=True) last_name = forms.CharField(required=True)
Say I have the following methods: public static void MyCoolMethod(params object[] allObjects) { }

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.