在一行C#中将多个元素添加到列表中

时间:2019-01-06 19:29:21

标签: c# collections

对于C#和一般的编程人员来说,我还很陌生,我正在尝试建立一个比萨饼的逻辑。

有没有一种功能或更有效的方式可以将多个元素添加到一行中的列表中。例如,现在我要制作一个5个顶级比萨饼:

Toppings.Add(top1);
Toppings.Add(top2);
Toppings.Add(top3);
Toppings.Add(top4);
Toppings.Add(top5);

有更好的方法吗?

在下面改进我的代码的任何建议也将不胜感激。

谢谢您的帮助!

using System.Collections.Generic;
using ZePizzaria.Domain.Enums;

namespace ZePizzaria.Domain.Models
{
  public class Pizza
  {
    public static Dictionary<EPizzaSize, double> Cost = new Dictionary<EPizzaSize, double>
    {
      {EPizzaSize.Mega, 25},
      {EPizzaSize.XLarge, 20},
      {EPizzaSize.Large, 15},
      {EPizzaSize.Medium, 10},
      {EPizzaSize.Small, 8.99}
    };

    public int PizzaId { get; set; }
    public ECrust Crust { get; set; }
    public double Price { get; set; }
    public EPizzaSize Size { get; set; }
    public List<EToppings> Toppings { get; set; }

    public Pizza()
    {
      Crust = ECrust.Regular;
      Price = Cost[EPizzaSize.Medium];
      Size = EPizzaSize.Medium;
      Toppings = new List<EToppings>
      {
        EToppings.Cheese,
        EToppings.Sauce
      };
    }

    public Pizza(ECrust crust, EPizzaSize size, EToppings top1)
      : this()
    {
      Crust = crust;
      Price = Cost[size];
      Size = size;
      Toppings.Add(top1);
    }

    public Pizza(ECrust crust, EPizzaSize size, EToppings top1, EToppings top2)
      : this()
    {
      Crust = crust;
      Size = size;
      Price = Cost[size];
      Toppings.Add(top1);
      Toppings.Add(top2);
    }

    public Pizza(ECrust crust, EPizzaSize size, EToppings top1, EToppings top2, EToppings top3)
            : this()
    {
      Crust = crust;
      Price = Cost[size];
      Size = size;
      Toppings.Add(top1);
      Toppings.Add(top2);
      Toppings.Add(top3);
    }
    public Pizza(ECrust crust, EPizzaSize size, EToppings top1, EToppings top2, EToppings top3, EToppings top4)
            : this()
    {
      Crust = crust;
      Price = Cost[size];
      Size = size;
      Toppings.Add(top1);
      Toppings.Add(top2);
      Toppings.Add(top3);
      Toppings.Add(top4);
    }
    public Pizza(ECrust crust, EPizzaSize size, EToppings top1, EToppings top2, EToppings top3, EToppings top4, EToppings top5)
            : this()
    {
      Crust = crust;
      Price = Cost[size];
      Size = size;
      Toppings.Add(top1);
      Toppings.Add(top2);
      Toppings.Add(top3);
      Toppings.Add(top4);
      Toppings.Add(top5);
    }
  }
} 

我正在尝试做这样的事情:

    Toppings.Add(top1, top2, top3, top4);

2 个答案:

答案 0 :(得分:1)

要回答您的确切问题:

查看List.AddRange(...)函数:https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.addrange?view=netframework-4.7.2

语法如下:

Toppings.AddRange(new List<EToppings>() {top1, top2, top3, top4});

要解决根本问题:

创建许多仅在额外的“ topping”参数数量上有所不同的构造函数有些混乱。您可以/应该通过C#中的params关键字将所有这些压缩为具有可变长度参数的单个构造函数。

public Pizza(ECrust crust, EPizzaSize size, params EToppings[] toppings) : this()
{
  Crust = crust;
  Price = Cost[size];
  Size = size;
  Toppings.AddRange(toppings);
}

答案 1 :(得分:0)

要使代码更整洁,可以传递列表。然后使用.AddRange方法添加列表,请参见.net 4.7 manual

Toppings.AddRange();

public Pizza(ECrust crust, EPizzaSize size, List<EToppings> lsToppings) : this()
{
  Crust = crust;
  Price = Cost[size];
  Size = size;
  Toppings.AddRange(lsToppings);
}

您可以使用以下内容列出列表

new List<EToppings>() {top1, top2, top3, top4}