A lawyer's Guide To Using Lists in C_Sharp

·

7 min read

So, you have decided to be brave and now find yourself learning C#. Well, as with all languages, we need a way to store values in a "container". To use Lists, you must import the System.Collections.Generic namespace.

e.g:

using System;
using System.Collections.Generic;

Having done that, we need to declare a new list and then use the .Add() method to add to the list.

List<string> names = new List<string> ();
names.Add("tony");
names.Add("James");
names.Add("sam");
names.Add("paul");
Console.WriteLine(String.Join(", ", names));

note we could have done the above by declaring and initializing the list at once:

List<string> names = new List<string> (){"tony", "james", "sam", "paul"};
Console.WriteLine(String.Join(", ", names));

The essence of this line Console.WriteLine(String.Join(", ", names));, is so that we have the print output look like this tony, James, sam, paul when we run our code.

The .Add() allows us to add to a list.

.Remove() removes from a list by providing the item we want to remove e.g names.Remove("tony"). will remove "tony" from the list.

List<string> names = new List<string> ();
names.Add("tony");
names.Add("james");
names.Add("sam");
names.Add("paul");
names.Remove("tony");
Console.WriteLine(String.Join(", ", names));

Sample Output will be:

james, sam, paul

.Insert() allows us to insert into a list. .Insert() requires 2 arguments: .Insert(<index position>, <new item>) e.g names.Insert(1, "Jane") will give our names list this output:

tony, Jane, james, sam, paul //sample output

We can see Jane now occupies the index position of One. To iterate over each item in the list, we can use the .foreach() method on out list.

foreach (var name in names)
{
    System.Console.WriteLine(name);
}
tony
Jane
james     //sample output
sam
paul

A beautiful thing about lists is we can have objects as items in the list. To show you how this works, we will create a Car class.

public class Car
        { 
            public int Id { get; set; }
            public string Name { get; set; }
        }

 //sample car section
            var cars = new List<Car> (){
                new Car(){Id = 1, Name = "Toyota"},
                new Car(){Id = 2, Name = "Honda"},
                new Car(){Id = 3, Name = "Nissan"}
            };

            foreach (var car in cars)
            {
                System.Console.WriteLine($"Id: {car.Id}, Name: {car.Name}");
            }

//sample Output
Id: 1, Name: Toyota
Id: 2, Name: Honda
Id: 3, Name: Nissan

We can also perform LINQ queries on lists. This is because "List" implements the IEnumerable interface. We must first import the Linq namespace as so:

using System;
using System.Collections.Generic;  
using System.Linq;
var result = from car in cars
     where car.Name == "Nissan"
     select car;

foreach(var car in the result)
Console.WriteLine(car.Id + ", " + car.Name);

//sample output
3, Nissan

Hope you can go kick ass with these few pointers.

Source Code:

using System;
using System.Collections.Generic; //this allows us to use Lists
using System.Linq;

namespace playground
{
    class Program
    {

        static void Main()
        {

            //sample names section
            // List<string> names = new List<string> ();
            // names.Add("tony");
            // names.Add("james");
            // names.Add("sam");
            // names.Add("paul");
            // names.Remove("tony");
            // Console.WriteLine(String.Join(", ", names));


            //another way to initialize and declare
            List<string> names = new List<string> (){"tony", "james", "sam", "paul"};

            names.Insert(1, "Jane");
            foreach (var name in names)
            {
                System.Console.WriteLine(name);
            }


            //sample car section
            var cars = new List<Car> (){
                new Car(){Id = 1, Name = "Toyota"},
                new Car(){Id = 2, Name = "Honda"},
                new Car(){Id = 3, Name = "Nissan"}
            };

            foreach (var car in cars)
            {
                System.Console.WriteLine($"Id: {car.Id}, Name: {car.Name}");
            }

            var result = from car in cars
                         where car.Name == "Nissan"
                         select car;


            foreach(var car in result)
            Console.WriteLine(car.Id + ", " + car.Name);

        }

        public class Car
        { 
            public int Id { get; set; }
            public string Name { get; set; }
        }

    }
}