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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:50:12+00:00 2026-05-26T02:50:12+00:00

How can i pass a list which is a list of DerivedObjects where the

  • 0

How can i pass a list which is a list of DerivedObjects where the Method is expecting a list of BaseObjects. I am converting the list .ToList<BaseClass>() and am wondering if there is a better way. My second problem is the syntax is incorrect. I am trying to pass the list byref and i am getting an error: 'ref' argument is not classified as a variable

How can I fix these two problem? thanks.

public class BaseClass { }
public class DerivedClass : BaseClass { }

class Program
{
    static void Main(string[] args)
    {
        List<DerivedClass> myDerivedList = new List<DerivedClass>();
        PassList(ref myDerivedList.ToList<BaseClass>());
// SYNTAX ERROR ABOVE IS - 'ref' argument is not classified as a variable

        Console.WriteLine(myDerivedList.Count);
    }

    public static void PassList(ref List<BaseClass> myList)
    {
        myList.Add(new DerivedClass());
        Console.WriteLine(myList.Count);
    }
}

Solved:

A method similar to this has solved my issue.

public static void PassList<T>(ref List<T> myList) where T : BaseClass
{
    if (myList == null) myList = new List<T>(); 
    // sorry, i know i left this out of the above example.

    var x = Activator.CreateInstance(typeof(T), new object[] {}) as T;
    myList.Add(x);
    Console.WriteLine(myList.Count);
}

Thank you to all who help across this question and from other SO questions.

  • 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-26T02:50:12+00:00Added an answer on May 26, 2026 at 2:50 am

    The ref part is easy: to pass an argument by reference, it has to be a variable, basically. So you can write:

    List<BaseClass> tmp = myDerivedList.ToList<BaseClass>();
    PassList(ref tmp);
    

    … but that won’t affect either the value of myDerivedList or contents itself.

    The ref here is pointless anyway, as you’re never changing the value of myList within the method anyway. It’s important to understand the difference between changing the value of a parameter and changing the contents of the object that the parameter value refers to. See my article on parameter passing for more details.

    Now as for why you can’t pass your list in – it’s to preserve type safety. Suppose you could do this, and we could write:

    List<OtherDerivedClass> list = new List<OtherDerivedClass>();
    PassList(list);
    

    Now that would be trying to add an instance of DerivedClass to a List<OtherDerivedClass> – that’s like adding an apple to a bunch of bananas… it doesn’t work! The C# compiler is preventing you from performing that unsafe operation – it won’t let you treat a bunch of bananas as a fruit bowl. Suppose we did have Fruit instead of BaseClass, and Banana / Apple as two derived classes, with PassList adding an Apple to the list it’s given:

    // This is fine - you can add an apple to a fruit bowl with no problems
    List<Fruit> fruitBowl = new List<Fruit>();
    PassList(fruitBowl);
    
    // This wouldn't compile because the compiler doesn't "know" that in PassList
    // you're only actually adding an apple.
    List<Apple> bagOfApples = new List<Apple>();
    PassList(bagOfApples);    
    
    // This is the dangerous situation, where you'd be trying to really violate
    // type safety, inserting a non-Banana into a bunch of bananas. But the compiler
    // can't tell the difference between this and the previous one, based only on
    // the fact that you're trying to convert a List<Banana or Apple> to List<Fruit>
    List<Banana> bunchOfBananas = new List<Banana>();
    PassList(bunchOfBananas );    
    

    C# 4 allows generic variance in certain situations – but it wouldn’t help in this particular case, as you’re doing something fundamentally unsafe. Generic variance is a fairly complicated topic though – as you’re still learning about how parameter passing works, I would strongly suggest that you leave it alone for the moment, until you’re more confident with the rest of the language.

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

Sidebar

Related Questions

I was wondering how I can pass either an ArrayList, List <int > or
You can pass special strings into jQuery's Datepicker class setDate() method like +7 which
Is there any way that I can pass arguments in selector? example: I have
Is there a way that I can pass in a reference to an object,
I have a method that uses a list which I need to pass into
I would like to know how I can pass a list with values in
I have controller which gets list of keys and i want to pass them
Case: Pass a unserializable parameter cross AppDomain. Following is some method which i want
I want to pass a list of products ( which is being created in
Which css hacks we use to make layout cross browser compatible Can pass W3C

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.