Adding multiple elements to a list in one line C#

Viewed 28695

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

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

Is there a better way of doing this?

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

Thank you for your help in advance!

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);
2 Answers

To answer your exact question:

Look into the List.AddRange(...) function: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.addrange?view=netframework-4.7.2

The syntax would look like this:

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

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

To make the code cleaner you could pass lists around. Then add the list with the .AddRange method, see .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);
}

You can make a list using the following

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