用List<T>属性初始化C#类

3

我需要一些帮助,了解如何在Main方法中使用一些示例值初始化以下对象,以执行某些操作。

由于我是C#的新手,请指导我在哪里可以获取此信息。

class MobOwner
{
   public string Name { get; set; }
   public List<string> Mobiles { get; set; }
}
6个回答

7
只需在构造函数中初始化它:
class MobOwner
{
    public string Name { get; set; }
    public List<string> Mobiles { get; set; }
    public MobOwner() {
        this.Mobiles = new List<string>();
    }
}

您还可以定义一个构造函数,直接将正确的值放入您的列表中:
class MobOwner
{
    public string Name { get; set; }
    public List<string> Mobiles { get; set; }
    public MobOwner(IEnumerable<string> values) {
        this.Mobiles = values.ToList();
    }
}

你可以像这样调用:new MobOwner(new[] { "Mario", "Hans", "Bernd" })

4
首先,我怀疑您是否真的想在Mobiles属性中使用set;:通常我们会添加/更新/删除列表中的项目,而不是整个分配列表。
  MobOwner sample = new MobOwner(...);

  sample.MobOwner.Add("123");
  sample.MobOwner.Add("456");
  sample.MobOwner.RemoveAt(1);
  sample.MobOwner[0] = "789";

  sample.MobOwner = null; // we, usually, don't want such code

实现可以是:
 class MobOwner {
   public string Name { get; set; } 
   public List<string> Mobiles { get; } = new List<string>();

   public MobOwner(string name, IEnumerable<string> mobiles): base() {
     if (null == name)
       throw new ArgumentNullException("name");

     if (null == mobiles)
       throw new ArgumentNullException("mobiles");

     Name = name;

     Mobiles.AddRange(mobiles); 
   }
 }

1
你可以创建一个实例并设置变量。
var owner = new MobOwner();
owner.Mobiles = new List<string>{"first", "second"};

或者像这样
var owner = new MobOwner {Mobiles = new List<string> {"first", "second"}};

推荐的方法是使用构造函数并将设置属性设置为私有。
class MobOwner
{
    public string Name { get; private set; }
    public List<string> Mobiles { get; private set; }
    // constructor
    public MobOwner(string name, List<string> mobiles)
    {
        Name = name;
        Mobiles = mobiles;
    }
}

0

这将创建一个 MobOwner 对象,其中包含一个项目列表。

MobOwner item = new MobOwner()
{
    Name = "foo",
    Mobiles = new List<string>() { "bar" }
};

另一种方法是添加构造函数以简化实例化。
class MobOwner
{
    public string Name { get; set; }
    public List<string> Mobiles { get; set; }

    public MobOwner(string Name, params string[] Mobiles) 
    {
        this.Name = Name;
        this.Mobiles = new List<string>(Mobiles);
    }
}

用法:

MobOwner item2 = new MobOwner("foo", "bar", "bar");

0
var mobOwner = new MobOwner()
   {
       Name = "name";
       Mobiles = new List<string>()
           {
               "mob1",
               "mob2",
               "mob3"
           };
    };

感谢大家的支持。如果我想要更多的项目,比如name2、mobA、mobB、mobC,我会使用Dmitriy Kovalenko的回复,这样可以吗? - rajeshnrh
@rajeshnrh:请注意,您应该真正熟悉构造函数,因为它们是为此目的而设计的。它们确保您无法使用无效状态初始化“MobOwner”。否则,您的类的用户必须知道他必须自己初始化此列表。他可能会尝试这样做:“var owner = new MobOwner(); owner.Mobiles.Add("mob");”,这将导致“NullReferenceException”。 - Tim Schmelter
如果您认为我的答案是正确的,请将其标记为正确。 - Dmitriy Kovalenko

0

如果我理解你的意图正确,你想在“Main”方法中初始化这些值。

构造函数是一种很好的方式,可以在创建类的实例时使用默认值来初始化属性。 但是,如果你想在其他地方初始化它们,请创建类的实例,然后可以为其公共成员赋值。像这样:

MobOwner mobOwner = new MobOwner();
mobOwner.Name = "Jimmy";
mobOwner.Mobiles = new List<string>{119, 011};

或者更现代的方式是可以像这样改变语法(虽然它们是相同的):

MobOwner mobOwner = new(){
    Name = "Jimmy",
    Mobiles = new List<string>{119, 011}
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接