(c#)访问/修改列表中的对象

本文关键字:列表 对象 修改 访问 | 更新日期: 2023-09-27 18:08:52

新来的,我已经学了大约一个月的c#了。

无论如何,我已经搜索StackOverflow几天了,现在找不到一个具体的答案来解决我的问题…

//Here's my Class
public class Guy
{
     public static int ID { get; set; }
     public static int LifeExpectancy { get; set; }
     public static bool Living { get; set; }
     public Guy(int id, int lifeExpectancy, bool living)
     {
         ID = id;
         LifeExpectancy = lifeExpectancy;
         Living = living;
     }
}

我要做的是创建一个特定数量的"someGuy"对象,然后使用此方法将它们放入公共列表…

public static List<Guy> Guys = new List<Guy>();
public static void makeSomeGuys(int howManyGuys)
{
    for (int i = 0, i <= howManyGuys; i++)
    {
        int id = i;
        int lifeExpectancy = 80;
        bool alive = true;
        Guys.Add(New Guy(id, lifeExpectancy, alive));
        Console.WriteLine("Made a new Guy {0}", id);         
    }
    return; 
}

问题按重要性排序:

如何访问一个特定的对象及其参数?(从列表中访问"家伙"。)

如何在另一个类中访问这个列表中的对象?(不是我绝对需要,我很好奇)

可以通过参数在列表中搜索对象吗?(而不是做…humanPopulation[number])

我是否应该为参数被修改的对象创建一个新的列表?(而不是将其留在原始列表中)

是否可以从列表中删除项?(一般来说,这是人们做的事情吗?

我真的只需要第一个问题的答案。剩下的只是额外的奖励。谢谢!

(c#)访问/修改列表中的对象

首先,您需要从Guy类的属性中删除静态修饰符,即:

public int ID { get; set; }
public int LifeExpectancy { get; set; }
public bool Living { get; set; }

,因为静态使属性成为类本身的属性,而不是类的实例(单个的"家伙")。

获取第一个人(第零个人)的预期寿命:

Console.WriteLine(Guys[0].LifeExpectancy);

获取第五个人的预期寿命:

Console.WriteLine(Guys[4].LifeExpectancy);
using System;
using System.Collections.Generic;
using System.Linq;
namespace test
{
    public class Guy
    {
        private int m_ID;
        private int m_LifeExpectancy;
        private bool m_Living;
        public int ID
        {
            get { return m_ID; }
            set { m_ID = value; }
        }
        public int LifeExpectancy
        {
            get { return m_LifeExpectancy; }
            set { m_LifeExpectancy = value; }
        }
        public bool Living
        {
            get { return m_Living; }
            set { m_Living = value; }
        }
        public Guy(int id, int lifeExpectancy, bool living)
        {
            ID = id;
            LifeExpectancy = lifeExpectancy;
            Living = living;
        }
    }
    public class MyFactory
    {
        public IList<Guy> MakeSomeGuys(int howManyGuys)
        {
            IList<Guy> localGuys = new List<Guy>();
            for (int i = 0; i <= howManyGuys; i++)
            {
                int id = i;
                int lifeExpectancy = 80;
                bool alive = true;
                localGuys.Add(new Guy(id, lifeExpectancy, alive));
                Console.WriteLine("Made a new Guy {0}", id);
            }

            return localGuys;
        }
    }
    public class program
    {
        public void Main()
        {
            MyFactory mf = new MyFactory();
            IList<Guy> guys = mf.MakeSomeGuys(5);
            //How do I access a specific object as well as its parameters? (Accessing from the list "Guys".)
            int GetFirstGuyId = guys.FirstOrDefault().ID; //LEARN LINQ
            //How do I access an object from this list in another class? (Not that I absolutely need to, I'm curious)
            //you need to learn about object oriented encapsulation for better understanding.
            //Can I search for an object in a list by using its parameters? (As opposed to doing something like...humanPopulation[number])
            Guy guyById = guys.Where(g => g.ID == 5).FirstOrDefault(); // returns the first match (need to learn lambda expression)
            //Should I create a new list for objects that have had their parameters modified? (As opposed to leaving it in the original list)
            // you need to learn about passing values by value / reference (by reference you already changing the original!).
            //Is it possible to remove items from a list? (Just in general, is that a thing people do? if so, why?)
            //yes
            guys.Remove(guyById);
        }
    }
}

您可能是c#和OO编程的新手,所以我在这个答案中包含了一些很好的链接。

仅针对问题1:

首先,您的Guy类属性没有正确封装。确保像本文中所示的那样正确地限定IDLifeExpectancyLiving属性的范围。

如果您想访问特定的项,即具有特定ID的Guy,您最好使用Dictionary之类的关联容器。

如果您对List容器感到满意,则需要在Guys上使用Find方法,如链接中的示例所示。您将在文档中注意到术语谓词,本链接将详细说明