3

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);
DavidG
  • 113,891
  • 12
  • 217
  • 223
user3050260
  • 41
  • 1
  • 1
  • 3
  • 1
    I'm voting to close this question as off-topic because it is asking for a code review rather than trying to solve a specific problem. Perhaps try here: https://codereview.stackexchange.com – David Jan 06 '19 at 19:32
  • 1
    *"Is there a better way of doing this?"* - Define "better". Those 5 lines of code are clear, concise, easy to support, and easy to debug. What's the problem you're looking to solve? Keep in mind that "fewer lines of code" is not always "better". Though, if you want, you can always write a method which accepts multiple values (check out the `params` keyword) and adds those to a collection. An extension method would be good for this. This would *abstract* the action of adding elements rather than *replace* it. – David Jan 06 '19 at 19:34
  • Agree that this is off-topic, but it's also a duplicate. – DavidG Jan 06 '19 at 19:49

2 Answers2

8

To answer your exact question:

Look into the List.AddRange(...) function: https://learn.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);
}
Brennen Sprimont
  • 1,564
  • 15
  • 28
1

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}
atoms
  • 2,993
  • 2
  • 22
  • 43