Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a List's Add and Append method?

Tags:

c#

Is there a difference between the .Append() and the .Add() method for lists in C#? I tried searching in Google and in the site but to my surprise no one asked it. My reason for asking is to know if one of the two methods are less performance intensive. I've been using the two methods interchangeably and I didn't see any differences to their function as they both added my object to the end of the list (and this is more or less the description visual studio gives you when you look at the method description).

Edit:

Hmmm I didn't think this mattered at first but I noticed I can use Append on an ASP.NET web application when I can't do that on a console application. So I was asking in the context of an ASP.NET Web App.

like image 981
kobowo Avatar asked Sep 16 '18 15:09

kobowo


People also ask

What is the difference between append() insert() and extend() method in Python lists?

In this article, we are going to discuss the difference between append (), insert (), and, extend () method in Python lists. It adds an element at the end of the list. The argument passed in the append function is added as a single element at end of the list and the length of the list is increased by 1.

Why does append () method add an entire item to a list?

It's because the .append () method adds the entire item to the end of the list. If the item is a sequence such as a list, dictionary, or tuple, the entire sequence will be added as a single item of the existing list. Here we have another example (below).

What is the difference between append and += in C++?

Using + adds the two lists and produces a new list. There is difference between append and +=. There's the fact that append adds one entry to the list, while += adds as many as there are in the other list (i.e. aliases to extend ). But he/she knows that already, judging by the way the question was written.

What is the difference between IEnumerable<T> append and list<t] add?

List<T> in C# only has the void Add (T item) method to modify the instance add a single item to the list. IEnumerable<T> Append (this IEnumerable<T> source, T element) on the other hand is an extension method defined on the IEnumerable<T> interface (which is implemented by all lists).


1 Answers

List<T> in C# only has the void Add(T item) method to modify the instance add a single item to the list.

IEnumerable<T> Append(this IEnumerable<T> source, T element) on the other hand is an extension method defined on the IEnumerable<T> interface (which is implemented by all lists). It does not modify the original list instance, but returns a new enumerable which will yield the specified element at the end of the sequence.

They cannot be used interchangably and behave differently with different outcomes and different side effects. Asking about their relative performance does not make sense as such.

var list = new List<string>(); list.Add("one"); list.Add("two"); // list contains: [ one, two ]  list.Append("three"); // list still contains: [ one, two ] 
like image 190
knittl Avatar answered Sep 28 '22 07:09

knittl