Difference between a List's Add and Append method?

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 ]

Add is a void.

Append returns an IEnumerable so you can

var x = new List<int>();
x.Add(1);
x = x.Append(2).Append(3).ToList();

Tags:

C#