有没有一种方法可以优化添加到具有相同值的列表中?

时间:2019-04-09 13:42:42

标签: c#

我必须将几个对象添加到包含相同信息的列表中-我想知道一种如何优化问题的方法。参见下面的代码。

“ GroupNumber”和“ IsAvailable”都将相同。

public class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
        List<ClassA> listAs = new List<ClassA>();
        listAs.Add(new ClassA() {GroupNumber = 1, Name = "PersonA", IsAvailable = true});
        listAs.Add(new ClassA() {GroupNumber = 1, Name = "PersonB", IsAvailable = true});
        listAs.Add(new ClassA() {GroupNumber = 1, Name = "PersonC", IsAvailable = true});
    }
}

public class ClassA
{
  public int GroupNumber {get; set;}
  public string Name {get; set;}
  public bool IsAvailable {get; set;}
}

我想知道是否存在一个快捷方式,在该快捷方式中,我只能添加“名称”,而使“ GroupNumber”和“ IsAvailable”保持不变。

listAs.Add(new ClassA() {Name = "PersonA"});
listAs.Add(new ClassA() {Name = "PersonA"});
listAs.Add(new ClassA() {Name = "PersonA"});

5 个答案:

答案 0 :(得分:4)

不。但是您可以创建一种减少重复数量的方法:

static void Main(string[] args)
{
    Console.WriteLine("Hello World!");
    List<ClassA> listAs = new List<ClassA>()
    {
        CreateAvailablePerson("PersonA"),
        CreateAvailablePerson("PersonB"),
        CreateAvailablePerson("PersonC"),
    };
}

private static ClassA CreateAvailablePerson(string name)
{
    return new ClassA() { GroupNumber = 1, Name = name, IsAvailable = true };
}

如果您使用的是C#7.0+,则可以使用local function来摆脱多余的类级方法

static void Main(string[] args)
{
    Console.WriteLine("Hello World!");
    List<ClassA> listAs = new List<ClassA>()
    {
        Create("PersonA"),
        Create("PersonB"),
        Create("PersonC"),
    };

    ClassA Create(string name) => new ClassA() { GroupNumber = 1, Name = name, IsAvailable = true };
}

例如,如果ClassA的默认状态可用,则您还可以默认将其初始化为true

public class ClassA
{
    public int GroupNumber { get; set; }
    public string Name { get; set; }
    public bool IsAvailable { get; set; } = true;
}

然后您可以执行new ClassA() { GroupNumber = 1, Name = name }。如果正常值是GroupNumber,当然也可以对1执行此操作。

答案 1 :(得分:2)

作为其他答案的替代方法,请在构造函数中使用默认参数:

public class ClassA
{
    public int GroupNumber {get; set;}
    public string Name {get; set;}
    public bool IsAvailable {get; set;}

    public ClassA(string name, int groupNumber = 1, bool isAvailable = true)
    {
        Name = name;
        GroupNumber = groupNumber;
        IsAvailable = isAvailable;
    }
}

答案 2 :(得分:1)

当然,您可以使用1个参数创建一个构造函数,并在默认情况下为其他字段设置所需的值。

 public ClassA (string name){
      GroupNumber = 100,
      Name = name,
      IsAvailable = true
}

如果要使用不同的GroupNumber值,请在类内部使用一些值初始化静态变量,然后在构造函数中对其进行递增。

答案 3 :(得分:1)

使用一个称为“ factory”的特殊类,如下所示:

using System;
using System.Linq;

namespace Solution
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var factory = new ClassAFactory(groupNumber: 1, isAvailable: true);
            var listAs = new[] { "PersonA", "PersonB", "PersonC" }
                               .Select(name => factory.CreateClassA(name))
                               .ToList();
        }
    }

    public class ClassA
    {
        public int GroupNumber { get; set; }
        public string Name { get; set; }
        public bool IsAvailable { get; set; }
    }

    public class ClassAFactory
    {
        private int GroupNumber { get; }
        public bool IsAvailable { get; }

        public ClassAFactory(int groupNumber, bool isAvailable)
        {
            GroupNumber = groupNumber;
            IsAvailable = isAvailable;
        }

        public ClassA CreateClassA(string name)
        {
            return new ClassA { GroupNumber = GroupNumber, Name = name, IsAvailable = IsAvailable };
        }
    }
}

答案 4 :(得分:0)

您可以设置默认值

public class ClassA
{
  public int GroupNumber {get; set;} = 1;
  public string Name {get; set;}
  public bool IsAvailable {get; set;} = true;
}