在一行C#中将多个元素添加到列表中 [英] Adding multiple elements to a list in one line C#

查看:61
本文介绍了在一行C#中将多个元素添加到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于C#和一般编程人员来说,我是一个相当陌生的人,我正在尝试使构建披萨的逻辑成为可能.

I am fairly new to C# and programming in general and I am trying to make the logic to build a pizza.

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

Is there a function or a more efficient way that I can add multiple elements to a list in one line. For example, to build a 5 topping pizza right now I do this:

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

有更好的方法吗?

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

Also any suggestions on improving my code below would be greatly appreciated.

谢谢您的帮助!

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);
    }
  }
} 

我正试图做这样的事情:

I am trying to potentially do something like this:

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

推荐答案

要回答您的确切问题:

查看List.AddRange(...)函数:语法如下:

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

要解决根本问题,请执行以下操作:

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

To resolve the underlying issue:

Creating many different constructors that only have a difference in the number of extra "topping" parameters is a bit messy. You could/should condense all of those into a single constructor with variable length arguments through the params keyword in C#.

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

这篇关于在一行C#中将多个元素添加到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆