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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:35:34+00:00 2026-06-08T13:35:34+00:00

Say I have a List<Objects> . I want to define the list of objects

  • 0

Say I have a List<Objects>. I want to define the list of objects in one method, and use them in several others.

Here’s the ways I’ve come up with and I’m looking for more or the correct way to do it.

  1. You can define List<Objects> in every method that uses it.
    • Pros: It works. No chance of getting the wrong variable.
    • Cons: Code duplication.
  2. You can use a private List<Objects> defined in the class and update it using (ref ListObjects)
    • Pros: I only have to define it once.
    • Cons: I feel like it’s messy and bad practice.
  3. You can pass List<Objects> as a parameter to the methods that use it.
    • Pros: Prevents code duplication
    • Cons: Have to make my populate functions return functions, and add parameters to my other methods. Possible conflicts with Events?

So that’s what I’ve come up with. I’m really not sure which to use or if there’s a better way to do this. Thoughts?

EDIT: Including some code as requested.

private List<MedicalPlan> medicalPlansList;

This is the list. It is a list that gets information from a database, here:

private void BindMedicalList()
{
   medicalPlansList = new MedicalPlanRepository().RetrieveAll().Where(x => x.Year == year).ToList();
}

Then it’s used to find objects in that list, such as

var result =
                    medicalPlansList.FirstOrDefault(
                        c => c.CoverageLevels.Any(p => p.Id == id));
  • 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-08T13:35:36+00:00Added an answer on June 8, 2026 at 1:35 pm

    This is, in general, how I’d do it. If you always use the same sequence of functions on a list, consider creating a chained function to handle that. You can also directly pass a function call inside one of the other function calls (as long as it returns a list), but that tends to look messy.

    public List<int> DoSomethingWithList(List<int> list)
    {
        //do stuff
        return list;
    }
    
    public List<int> DoSomethingElseWithList(List<int> list)
    {
        //do other stuff
        return list;
    }
    
    public void SomeOtherFunction(string[] args)
    {
        var list = new List<int>() { 1, 2, 3, 4 }; //create list
        list = DoSomethingWithList(list); //change list
        list = DoSomethingElseWithList(list); //change list further
    }
    

    If you are working with an object that has a List<T> field, I’d do like this:

    public class MyBigClass
    {
        private List<int> myList;
        public MyBigClass()
        {
            //instantiate list in constructor
            myList = new List<int>() { 1, 2, 3, 4 }; 
        }
    
        public void PublicListAdder(int val)
        {
            myList.Add(val);
        }
    
        private void PrivateListCleaner()
        {
            //remove all even numbers, just an example
            myList.RemoveAll(x => x % 2 == 0);
        }
    }
    

    You rarely need to use ref in C#, because it automatically handles pointers for you. You are (usually) not passing around a struct, you are passing around an object reference (which basically is a pointer).

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

Sidebar

Related Questions

I have a list of objects, say List<Topic> I want to serialize them. I
Let's say I have an array of primitives or a list of objects, doesn't
Say, I have a fonction that operates a list of base objects. But I
Let's say I have an array list of objects, and each object has the
Say I have a List of objects, and the object has a string property.
Lets say that I have a list of objects like so: public class FlatModel
Lets assume I have a list of objects in an array and i want
I have an array with a list of objects. I want to split this
Say I want to have an input form where one of the inputs is
Let's say I have a list of books I want to render in a

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.