TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# List Add Method

These C# example programs use the Add method on the List type. They add value types and reference types.

Add places an element at the end of a List.

It handles both value types and reference types. The Add instance method on the List type has internal logic that allows you to quickly add elements to the end of the collection.

Example. Here we declare a new List with an integer (Int32) type parameter. We then use the Add method four times. This example shows how to create a new List of unspecified size, and add four prime numbers to it.

IntPrimes

Note: The angle brackets are part of the declaration type, not numeric operators. They are treated differently in the language.

AddRange: For adding many elements at once, you can use the AddRange method on List for less code.

AddRange

Based on:

.NET 4

C# program that uses Add method

using System.Collections.Generic;

class Program
{
    static void Main()
    {
	// Add first four numbers to the List.
	List<int> primes = new List<int>();
	primes.Add(2);
	primes.Add(3);
	primes.Add(5);
	primes.Add(7);
    }
}

Example 2. Next we declare a custom class, and then add instances of it to a new List. The type in between the angle brackets is the name of the new class Test. This means the List will only contain Test objects.

And: Because the Test type is a reference type, the List stores references to the Test objects on the managed heap.

C# program that adds objects to List

using System.Collections.Generic;

class Program
{
    static void Main()
    {
	// Add three objects to a List.
	List<Test> list = new List<Test>();
	list.Add(new Test(1, 2));
	list.Add(new Test(3, 4));
	list.Add(new Test(5, 6));
    }

    class Test
    {
	int _a;
	int _b;
	public Test(int a, int b)
	{
	    _a = a;
	    _b = b;
	}
    };
}

Internals. When you call Add each time, the EnsureCapacity method may be invoked. If you are adding an element that will not fit in the array size, the array is resized with the Array.Copy method.

Array.Copy

Resizing an array is slow, but if you are using a reference type in your List, the references themselves are small and will not require excessive copying. This is an advantage to reference types.

Capacity. Because the List collection is dynamically resized, it must manage its capacity in a way that is algorithmically efficient. It first allocates the internal array with a length of 4.

Then: It doubles the array size each time it runs out of room during an Add call.

Summary. Add receives a single parameter of the same type as is contained in the List. We noted the usage of the Add method with integers and object references, and also mentioned how the capacity of the List is managed internally.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf