C# Adding Multiple Items to List

Add multiple items to a list

Code check:

This is offtopic here but the people over at CodeReview are more than happy to help you.

I strongly suggest you to do so, there are several things that need attention in your code.
Likewise I suggest that you do start reading tutorials since there is really no good reason not to do so.


As you said yourself: you need a list of items. The way it is now you only store a reference to one item. Lucky there is exactly that to hold a group of related objects: a List.

Lists are very straightforward to use but take a look at the related documentation anyway.

A very simple example to keep multiple bikes in a list:

List<Motorbike> bikes = new List<Motorbike>();

bikes.add(new Bike { make = "Honda", color = "brown" });
bikes.add(new Bike { make = "Vroom", color = "red" });

And to iterate over the list you can use the foreach statement:

foreach(var bike in bikes) {

Adding multiple elements to a list in one line C#

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;

How can i insert multiple values together in a list

You could do it as follows:

 someStringsList.AddRange(new string[] { "text1", "text2", "text3", "text4" });

Adding multiple values on list

Should be as easy as

var myList = new List<List<string>>()
new List<string> { "a", "b", "c", "d", "e", "f", "g" },
new List<string> { "h", "i", "j", "k", "l", "m", "n" },
new List<string> { "a1", "a2", "a3" },
// OR

var myarray = new[]
new[] { "a", "b", "c", "d", "e", "f", "g" },
new[] { "h", "i", "j", "k", "l", "m", "n" },
new[] { "a1", "a2", "a3" },

Additional Resources

Object and Collection Initializers (C# Programming Guide)

C# lets you instantiate an object or collection and perform member
assignments in a single statement.

Collection initializers

Collection initializers let you specify one or more element
initializers when you initialize a collection type that implements
IEnumerable and has Add with the appropriate signature as an instance
method or an extension method. The element initializers can be a
simple value, an expression, or an object initializer. By using a
collection initializer, you do not have to specify multiple calls; the
compiler adds the calls automatically.

how to add multiple items in list for a fix dictionary key

So you want a single element in the dictionary with the key being set to "X" and the value set to the list of students?

var students = data
.Select(d => new Student
Name = d.Key.Split('!')[0],
Section = d.Key.Split('!')[1],
Age = d.Value

finalResult.Add(dictKey, students);

How to add multiple items in a list dynamically using linq in c#

You are working on the same instance of billObj each time you call the method addNewBillionaire. The List would keep a reference to the same copy, which is updated when you call the method the second time.

To resolve it,You need to reinitialize billObj in your method (as given in the comments).

public static void addNewBillionaire() 
billObj = new Billionaire();
Console.WriteLine("Enter name:");
billObj.Name = Console.ReadLine();
Console.WriteLine("Enter the income:");
billObj.Income = int.Parse(Console.ReadLine());
Console.WriteLine("Enter country:");
billObj.Country = Console.ReadLine();

If billObj isn't used elsewhere, a better approach would be to make billObj as local variable for the method

Related Topics

Leave a reply