Difference between a List's Add and Append method?

c#
23,747

Solution 1

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 ]

Solution 2

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

You might like to see Wikipedia:Fluent interface for further information on how chaining functions works.

Share:
23,747

Related videos on Youtube

kobowo
Author by

kobowo

Mobile app developer for about a year. Trying to learn cross-platform development frameworks such as Flutter and React Native.

Updated on June 07, 2021

Comments

  • kobowo
    kobowo almost 3 years

    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.

    • JLRishe
      JLRishe over 5 years
      The main difference is that Lists don't have an Append method AFAIK. Can you give an example of your code?
    • kobowo
      kobowo over 5 years
      @JLRishe I hadn't thought of that. Thanks! I hadn't thought of the inheritance betwen a List and IEnumerable as pointed out by knittl..
  • kobowo
    kobowo over 5 years
    Weird.. okay so the reason I asked this was in the context of ASP.NET. idk if I should have mentioned that part or not but creating a list there allowed me to append to an empty List there which apparently on a console app in C# won't allow me to use the same method (Append).
  • JLRishe
    JLRishe over 5 years
    @kobowo In order to use the Linq extensions (such as Append), you'll typically need to have a using System.Linq; in the file where you intend to use them. I suspect that's why you're seeing supposed inconsistencies in Appends availability. You should be able to use it in both console apps and ASP.NET just fine.
  • kobowo
    kobowo over 5 years
    @JLRishe I tried adding the line but it didn't allow me to use Append on my console app.. I make it a habit to always use the ctrl + . functionality in vs to check if I'm missing a using statement so yeah...
  • JLRishe
    JLRishe over 5 years
    @kobowo Looks like it's only available in newer versions of .NET. What version of .NET is your console app using?
  • kobowo
    kobowo over 5 years
    @JLRishe .NET 4.5 if I'm not mistaken? But anyway it's not really a problem with my project...I honestly asked this out of pure curiosity. If it works in newer versions then I'll take note of it. Anyways thanks! my curiosity was sated with everyone's answer :)
  • JLRishe
    JLRishe over 5 years
    @kobowo .Append is included from .NET Framework 4.7.1 onward.
  • STEEL
    STEEL about 3 years
    much clear and easy to understand. Add modifies original data while Append returns new array without modifying original data