Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Lists in C#

In this article, we will learn about the List class.

The List is a collection of strongly typed objects that can be accessed by index and having methods for sorting, searching, and modifying list. It comes under System.Collection.Generic namespace.

Creating a List

We create a list in C# using the following syntax:
List<T> l1 = new List<T>();
<T> here denotes any datatype of which we want to create a list of.


Properties of List

  • Count: This property gets the number of nodes actually contained in the List.</li>
  • Capacity: Gets or sets the total number of elements the internal data structure can hold without resizing..
  • List[Int32]: Gets or sets the element at the specified index.
  • </ul>

    Methods of List

    • Add(T): Adds the element at the end of the List.</li>
    • AddRange(IEnumerable)</code>: Adds the elements of the specified collection to the end of the List.</li>
    • Contains(T): Determines whether a value is in the List.</li>
    • Remove(T): Removes the first occurrence of the specified value from the List.</li>
    • RemoveAt(ind): Removes the element at the specified index.
    • Sort(): Sorts the elements in the entire List using the default comparer.</li>
    • Insert(ind, T): Inserts T at the specified index.
    • Clear(): Removes all the elements from the List.</li> </ul>

      List Operation Examples:

      Different List operations are demonstrated in the example below:

      using System;
      using System.Collections.Generic;
      namespace ListDemo {
          class Example {
              static void Main(string[] args) {
                  List < int > L = new List < int > ();
                  L.Add(5);          // Adds to the end
                  L.Add(2);          
                  L.Add(4);
                  L.Add(9);
                  foreach(int i in L) {
                      Console.Write(i + " ");
                  }
                  Console.WriteLine();
                  L.Sort();                // Sorts the list in ascending order
                  foreach(int i in L) {
                      Console.Write(i + " ");
                  }
                  Console.WriteLine();
                  L.RemoveAt(1);           // Removes the elements from the 1st index
                  L.Remove(9);             // Removes first occurenece of 9
                  foreach(int i in L) {
                      Console.Write(i + " ");
                  }
                  Console.WriteLine();
                  Console.WriteLine(L.Contains(2));    // Checks if 2 is present in the list
                  Console.WriteLine(L.Count);          // Count returns the number of elements present
              }
          }
      }
      

      Output:

      5 2 4 9
      2 4 5 9
      2 5
      True
      2

      Task

      In the editor below, perform the different tasks as directed.

Start solving Lists in C# on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test