TheDeveloperBlog.com

Home | Contact Us

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

<< Back to C-SHARP

C# ToList Extension Method

Use and benchmark the ToList extension method from System.Linq. Convert IEnumerables to Lists.
ToList. This extension method converts collections (IEnumerables) to List instances. It is fast and easy-to-remember. It returns a List instance with the appropriate elements.ListIEnumerableToArrayToDictionary
ToList creates a new List internally, with the List constructor. So we can replace its usage (for converting an array) with the constructor directly.
First example. This program creates an array with an array initializer. It then converts that object data into a List instance. The System.Linq namespace is included.Array

Tip: System.Linq allows us to invoke the ToList extension method on the array reference.

LINQ

4 elements: The program uses an array initializer to specify that the array has 4 elements.

Initialize Array

Next: The ToList extension method is called on that array reference. ToList is an extension method from the System.Linq namespace.

Extension

And: It is called in the same way as an instance method is called. It returns a new List of string instances.

StringsConsole
C# program that uses ToList extension method using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { // // Use this input string[] array. // ... Convert it to a List with the ToList extension. // string[] array = new string[] { "A", "B", "C", "D" }; List<string> list = array.ToList(); // // Display the list. // Console.WriteLine(list.Count); foreach (string value in list) { Console.WriteLine(value); } } } Output 4 A B C D
Benchmark. We can compare the performance of ToList and the List() constructor. ToList has some extra indirection, so this will likely cause a small performance penalty.

Version 1: This version of the code uses ToList to get a list from an array. The array creation is not included in the time.

Version 2: Here we use the List constructor to get a List from the array. The result is same as in version 1.

Result: The List() constructor performs slightly faster on an int array. When possible, it is worth using the List constructor.

C# program that benchmarks ToList using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; class Program { const int _max = 1000000; static void Main() { int[] array = new int[] { 10, 20, 30, 40, int.MaxValue, int.MinValue }; var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { // Version 1: use ToList. var list = array.ToList(); if (list.Count != 6) { return; } } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { // Version 2: use List constructor. var list = new List<int>(array); if (list.Count != 6) { return; } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.Read(); } } Output 108.43 ns ToList 102.45 ns List constructor (new List)
Internals. The ToList method internally checks its parameter for null, which will occur if you try to use ToList on a null reference.

Then: ToList invokes the List type constructor with the instance parameter. So it is a wrapper method for the List constructor.

Null

Tip: This means that using "instance.ToList()" is similar to using "new List<T>(instance)" where T is the type.

Constructor
A summary. We examined the ToList extension method. We noted its usage in a trivial program. We evaluated the method's implementation.
Some final notes. Internally the ToList extension method invokes the List type constructor. Its performance characteristics are about the same.Convert
© TheDeveloperBlog.com
The Dev Codes

Related Links:


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