How To Find An Item In The List In C#

How to find an item in the listBanner Image

Introduction

When we have a list of items at some point we may want to find an item or object in the list. C# has built-in methods to do this, like List Find, List IndexOf, List FindAll, Enumerable Where, and Dictionary. But it is also important to understand the most basic find method we can have so that we can understand how helpful these methods are. So as a baseline case, we will first look at just looping through the items one by one until we find the item we're looking for.

Base Case Loop Method

This is simply looping through each item until we find a match on the desired object by some identification. In this example, we'll look at a list of cars and loop through and find a matching VIN. See the example.

Base Case Loop Method Example Code

List<Car> carList = GetInitialCarList();//Get intial hardcored list
int vinNumberTofind = 5;//VIN selected by a user to find.
Car car = FindInListByLoop(vinNumberTofind, carList);//Find the car object by vinNumber through a loop method
Console.WriteLine(car);//Write to screen

List<Car> GetInitialCarList()
{
    List<Car> carList = new List<Car>();//Create new Car List is empty
    carList.Add(new Car(1, "black", 4, "SuperT", "SUV39", 2020));//Create new Car object
    carList.Add(new Car(2, "yellow", 4, "SuperRX", "Compact4", 2010));//Create new Car object
    carList.Add(new Car(3, "red", 4, "SuperH", "Truck3", 2011));//Create new Car object
    carList.Add(new Car(4, "blue", 4, "HyperT", "SUV39", 2021));//Create new Car object
    carList.Add(new Car(5, "dark blue", 4, "HyperF", "Sedan89", 2013));//Create new Car object
    carList.Add(new Car(6, "red", 4, "HyperX", "Truck6", 2020));//Create new Car object
    carList.Add(new Car(7, "yellow", 4, "SuperT", "MiniVan9", 2011));//Create new Car object
    carList.Add(new Car(8, "gray", 4, "SuperF", "Van28", 2022));//Create new Car object
    carList.Add(new Car(9, "white", 4, "SuperRX", "SUV9", 2000));//Create new Car object
    carList.Add(new Car(10, "white", 4, "SuperB", "Sedan12", 2005));//Create new Car object
    return carList;
}

Car FindInListByLoop(int vinNumber, List<Car> carList)
{
    foreach (Car car in carList)//Loop through all objects until a match is found
    {
        if (car.VinNumber == vinNumber)//If vinNumber is a match of what we're looking for then return that object
        {
            return car;//Return found object
        }
    }
    return null;//return null if nothing is found. In a proper code we should be prepared to handle this case
}

public class Car
{
    public Car(int vinNumber, string color, int doorNumber, string make, string model, int year)
    {
        VinNumber = vinNumber;
        Color = color;
        DoorNumber = doorNumber;
        Make = make;
        Model = model;
        Year = year;
    }
    public int VinNumber { get; set; }
    public string Color { get; set; }
    public int DoorNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
    public override string? ToString()//override ToString so that Console.Writeline display below instead of the default
    {
        return $"VinNumber:{VinNumber}, Color:{Color}, DoorNumber:{DoorNumber}, Make:{Make}, Model:{Model}, Year:{Year}";
    }
}
Code Output
VinNumber:5, Color:dark blue, DoorNumber:4, Make:HyperF, Model:Sedan89, Year:2013

This gives us we want but there are better methods than this for finding a object in a list.

Base Case Loop Method Speed Test

Next, we'll test the speed of this method. This test selects at random an id(VIN) to match in the list of 100 thousand objects. This will happen 50 thousand times will equal one test then the test will repeat 10 times to get the average speed. Let's see the base case result.

Base Case Loop Method Speed Test Example Code
using System.Diagnostics;
int numberOfTests = 10;//Number of tests 
List<double> testSpeedList = new List<double>();

for (int i = 0; i < numberOfTests; i++)
{
    testSpeedList.Add(BaseCaseLoopMethodSpeedTest());
}
Console.WriteLine($"Base Case Loop Method Average speed:{Math.Round(testSpeedList.Average())}ms, In {numberOfTests} tests");

double BaseCaseLoopMethodSpeedTest()
{
    List<Car> carList = GetInitialCarList();//Get intial random generated list
    int numberOfFunctionCalls = 50000;//Number of function calls made
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();//Start the Stopwatch timer
    Car selectedCar = null;
    for (int i = 0; i < numberOfFunctionCalls; i++)
    {
        stopwatch.Stop();
        int selectedVinNumber = GetIdToSearchFor(carList);//Not counting this in the time
        stopwatch.Start();
        selectedCar = FindInListByLoop(selectedVinNumber, carList);
    }
    stopwatch.Stop();//Stop the Stopwatch timer
    Console.WriteLine($"Function calls:{numberOfFunctionCalls}, In {stopwatch.Elapsed.Minutes}m {stopwatch.Elapsed.Seconds}s {stopwatch.Elapsed.Milliseconds}ms");
    return stopwatch.Elapsed.TotalMilliseconds;
}
Supporting Code
List<Car> GetInitialCarList()
{
    List<Car> carList = new List<Car>();//Create new Car List is empty
    int numberOfObjectsToCreate = 100000;
    int maxNumberOfVinNumber = 999999999;
    int maxNumberOfColorCharacters = 4;
    int maxNumberofWheels = 4;
    int maxNumberOfMakeCharacters = 10;
    int maxNumberOfModelCharacters = 10;
    int maxNumberOfYear = 4;
    for (int i = 0; i < numberOfObjectsToCreate; i++)
    {
        carList.Add(new Car(GetRandomInt(maxNumberOfVinNumber), GetRandomText(maxNumberOfColorCharacters), GetRandomInt(maxNumberofWheels), GetRandomText(maxNumberOfMakeCharacters), GetRandomText(maxNumberOfModelCharacters), GetRandomInt(maxNumberOfYear)));//Add a new Car to the list
    }
    return carList;
}

int GetIdToSearchFor(List<Car> carList)
{
    int randomIndex = GetRandomInt(carList.Count - 1);//Get random index in the car list
    return carList[randomIndex].VinNumber;//return vin number of the random car picked
}

Car FindInListByLoop(int vinNumber, List<Car> carList)
{
    foreach (Car car in carList)//Loop through all objects until a match is found
    {
        if (car.VinNumber == vinNumber)//If the VIN is a match of what we're looking for then return that object
        {
            return car;//Return found object
        }
    }
    return null;//return null if nothing is found. In a proper code, we should be prepared to handle this case
}

string GetRandomText(int numberOfCharacters)
{
    string finalOutput = "";
    string alphbet = "abcedfghijklmnopqrstuvwxyz";
    for (int i = 0; i < numberOfCharacters; i++)
    {
        int randomIndex = GetRandomInt(alphbet.Length - 1);//Get random number between 0 and the string's length - 1
        finalOutput += alphbet[randomIndex];//Pick an index based on the random index
    }
    return finalOutput;
}

int GetRandomInt(int maxNumber)
{
    Random random = new Random();//Create Random class
    int randomInt = random.Next(maxNumber);//Get a random number between 0 and the maxnumber
    return randomInt;
}

public class Car
{
    public Car(int vinNumber, string color, int doorNumber, string make, string model, int year)
    {
        VinNumber = vinNumber;
        Color = color;
        DoorNumber = doorNumber;
        Make = make;
        Model = model;
        Year = year;
    }
    public int VinNumber { get; set; }
    public string Color { get; set; }
    public int DoorNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
    public override string? ToString()//override ToString so that Console.Writeline display below instead of the default
    {
        return $"VinNumber:{VinNumber}, Color:{Color}, DoorNumber:{DoorNumber}, Make:{Make}, Model:{Model}, Year:{Year}";
    }
}
Code Output
Function calls:50000, In 0m 14s 592ms
Function calls:50000, In 0m 14s 543ms
Function calls:50000, In 0m 14s 663ms
Function calls:50000, In 0m 14s 790ms
Function calls:50000, In 0m 14s 691ms
Function calls:50000, In 0m 14s 760ms
Function calls:50000, In 0m 14s 773ms
Function calls:50000, In 0m 14s 636ms
Function calls:50000, In 0m 15s 307ms
Function calls:50000, In 0m 14s 627ms
Base Case Loop Method Average speed:14739ms, In 10 tests

With a base speed of around 14 seconds, we can expect this to be the slowest speed.

How Readable Is The Base Case Loop Method

This reads well as there is only one conditional statement. If there's match then the object is returned. If not found then null is returned.

How Concise Is The Base Case Loop Method

This takes up serval lines so it's not very compact but easy to understand. The other methods would condense to one line.

List Find Method

The list comes with a built-in method that searches the list and finds the object and returns the object if there's a match on the condition given. This is a handy function to have and saves us from writing our function. Let's see an example of this.

List Find Method Example
List<Car> carList = GetInitialCarList();//Get initial hardcoded list
int vinNumberTofind = 5;//VIN selected by a user to find
Car car = FindInListByFind(vinNumberTofind, carList);//Find the car object by vinNumber through a loop method
Console.WriteLine(car);//Write to screen

Car FindInListByFind(int vinNumber, List<Car> carList)
{
    return carList.Find(x => x.VinNumber == vinNumber);//Pass the condition to find the object and return it. A null can be returned
}

Supporting Code
List<Car> GetInitialCarList()
{
    List<Car> carList = new List<Car>();//Create new Car List as empty
    carList.Add(new Car(1, "black", 4, "SuperT", "SUV39", 2020));//Create new Car object
    carList.Add(new Car(2, "yellow", 4, "SuperRX", "Compact4", 2010));//Create new Car object
    carList.Add(new Car(3, "red", 4, "SuperH", "Truck3", 2011));//Create new Car object
    carList.Add(new Car(4, "blue", 4, "HyperT", "SUV39", 2021));//Create new Car object
    carList.Add(new Car(5, "dark blue", 4, "HyperF", "Sedan89", 2013));//Create new Car object
    carList.Add(new Car(6, "red", 4, "HyperX", "Truck6", 2020));//Create new Car object
    carList.Add(new Car(7, "yellow", 4, "SuperT", "MiniVan9", 2011));//Create new Car object
    carList.Add(new Car(8, "gray", 4, "SuperF", "Van28", 2022));//Create new Car object
    carList.Add(new Car(9, "white", 4, "SuperRX", "SUV9", 2000));//Create new Car object
    carList.Add(new Car(10, "white", 4, "SuperB", "Sedan12", 2005));//Create new Car object
    return carList;
}

public class Car
{
    public Car(int vinNumber, string color, int doorNumber, string make, string model, int year)
    {
        VinNumber = vinNumber;
        Color = color;
        DoorNumber = doorNumber;
        Make = make;
        Model = model;
        Year = year;
    }
    public int VinNumber { get; set; }
    public string Color { get; set; }
    public int DoorNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
    public override string? ToString()//override ToString so that Console.Writeline display below instead of the default
    {
        return $"VinNumber:{VinNumber}, Color:{Color}, DoorNumber:{DoorNumber}, Make:{Make}, Model:{Model}, Year:{Year}";
    }
}
Code Output
VinNumber:5, Color:dark blue, DoorNumber:4, Make:HyperF, Model:Sedan89, Year:2013

This gives us the same result as our baseline function so we know that this is working as expected.

List Find Method Speed Test

Based on the baseline test, we will test the speed of the list find function and see how it compares to the baseline. The speed is a determining factor in these tests because the user is most impacted by the speed. Let's examine the code.

List Find Method Speed Test Example Code
using System.Diagnostics;
int numberOfTests = 10;//Number of tests 
List<double> testSpeedList = new List<double>();
for (int i = 0; i < numberOfTests; i++)
{
    testSpeedList.Add(ListFindMethodSpeedTest());
}
Console.WriteLine($"List Find Loop Method Average speed:{Math.Round(testSpeedList.Average())}ms, In {numberOfTests} tests");

double ListFindMethodSpeedTest()
{
    List<Car> carList = GetInitialCarList();//Get intial random generated list
    int numberOfFunctionCalls = 50000;//Number of function calls made
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();//Start the Stopwatch timer
    Car selectedCar = null;
    for (int i = 0; i < numberOfFunctionCalls; i++)
    {
        stopwatch.Stop();
        int selectedVinNumber = GetIdToSearchFor(carList);//Not counting this in the time
        stopwatch.Start();
        selectedCar = FindInListByFind(selectedVinNumber, carList);
    }
    stopwatch.Stop();//Stop the Stopwatch timer
    Console.WriteLine($"Function calls:{numberOfFunctionCalls}, In {stopwatch.Elapsed.Minutes}m {stopwatch.Elapsed.Seconds}s {stopwatch.Elapsed.Milliseconds}ms");
    return stopwatch.Elapsed.TotalMilliseconds;
}
Supporting Code
List<Car> GetInitialCarList()
{
    List<Car> carList = new List<Car>();//Create new Car List as empty
    int numberOfObjectsToCreate = 100000;
    int maxNumberOfVinNumber = 999999999;
    int maxNumberOfColorCharacters = 4;
    int maxNumberofWheels = 4;
    int maxNumberOfMakeCharacters = 10;
    int maxNumberOfModelCharacters = 10;
    int maxNumberOfYear = 4;
    for (int i = 0; i < numberOfObjectsToCreate; i++)
    {
        carList.Add(new Car(GetRandomInt(maxNumberOfVinNumber), GetRandomText(maxNumberOfColorCharacters), GetRandomInt(maxNumberofWheels), GetRandomText(maxNumberOfMakeCharacters), GetRandomText(maxNumberOfModelCharacters), GetRandomInt(maxNumberOfYear)));//Add a new Car to the list
    }
    return carList;
}

int GetIdToSearchFor(List<Car> carList)
{
    int randomIndex = GetRandomInt(carList.Count - 1);//Get random index in the car list
    return carList[randomIndex].VinNumber;//return vin number of the random car picked
}

Car FindInListByFind(int vinNumber, List<Car> carList)
{
    return carList.Find(x => x.VinNumber == vinNumber);
}

string GetRandomText(int numberOfCharacters)
{
    string finalOutput = "";
    string alphbet = "abcedfghijklmnopqrstuvwxyz";
    for (int i = 0; i < numberOfCharacters; i++)
    {
        int randomIndex = GetRandomInt(alphbet.Length - 1);//Get random number between 0 and the string's length - 1
        finalOutput += alphbet[randomIndex];//Pick an index based on the random index
    }
    return finalOutput;
}

int GetRandomInt(int maxNumber)
{
    Random random = new Random();//Create Random class
    int randomInt = random.Next(maxNumber);//Get a random number between 0 and the maxnumber
    return randomInt;
}

public class Car
{
    public Car(int vinNumber, string color, int doorNumber, string make, string model, int year)
    {
        VinNumber = vinNumber;
        Color = color;
        DoorNumber = doorNumber;
        Make = make;
        Model = model;
        Year = year;
    }

    public int VinNumber { get; set; }
    public string Color { get; set; }
    public int DoorNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

    public override string? ToString()//override ToString so that Console.Writeline display below instead of the default
    {
        return $"VinNumber:{VinNumber}, Color:{Color}, DoorNumber:{DoorNumber}, Make:{Make}, Model:{Model}, Year:{Year}";
    }
}
Code Output
  Function calls:50000, In 0m 11s 757ms
  Function calls:50000, In 0m 11s 269ms
  Function calls:50000, In 0m 11s 711ms
  Function calls:50000, In 0m 11s 804ms
  Function calls:50000, In 0m 11s 598ms
  Function calls:50000, In 0m 11s 799ms
  Function calls:50000, In 0m 11s 375ms
  Function calls:50000, In 0m 11s 398ms
  Function calls:50000, In 0m 11s 485ms
  Function calls:50000, In 0m 11s 423ms
  List Find Loop Method Average speed:11562ms, In 10 tests

With an average speed of 11 seconds, this is an improvement over the loop method. This makes this the front-runner method so far.

How Readable Is The List Find Method

This is very readable but the syntax for adding the conditional may be new to some but it is not too hard to pick up.

How Concise Is The List Find Method

This is very compact and the function is named well with one word Find. It takes up only one line with is a great and the conditional statement can be added in the same line which is also good.

C# Programming for Unity Game Development Specialization

COURSERA

C# Programming for Unity Game Development Specialization

4.7 (2,230 reviews)

Beginner level

No previous experience is necessary

3-months at 10 hours a week (At your pace)

$59 / month or audit, and financial aid available

If you want to learn more about C# then check out this course on Coursera. It's a 3 month course to take you through the basics of C#.

You can get started with programming using C# and apply it to Unity games in this beginner-level program. Facilitated by the University of Colorado, the program offers a chance to grasp the basics of C# and utilize the knowledge to develop Unity games.

[Full Disclosure: As an affiliate, I receive compensation if you purchase through this link]

Start Your 7-day Free Trial

List IndexOf Method

IndexOf for list returns an index where the object exists then we use the indexer function and pass an object to get the object. It is a two-step process. There is also some setup involved because we need to override the Equals and Hashcode So let's look at an example.

List IndexOf Method Example Code
List<Car> carList = GetInitialCarList();//Get initial hardcoded list
int vinNumberTofind = 5;//VIN selected by a user to find

Car car = FindInListByIndexOf(vinNumberTofind, carList);//Find the car object by vinNumber through a loop method
Console.WriteLine(car);//Write to screen

Car FindInListByIndexOf(int vinNumber, List<Car> carList)
{
    Car car = new Car(vinNumber);
    int foundCarIndex = carList.IndexOf(car);
    return carList[foundCarIndex];
}
Supporting Code
List<Car> GetInitialCarList()
{
    List<Car> carList = new List<Car>();//Create new Car List as empty
    carList.Add(new Car(1, "black", 4, "SuperT", "SUV39", 2020));//Create new Car object
    carList.Add(new Car(2, "yellow", 4, "SuperRX", "Compact4", 2010));//Create new Car object
    carList.Add(new Car(3, "red", 4, "SuperH", "Truck3", 2011));//Create new Car object
    carList.Add(new Car(4, "blue", 4, "HyperT", "SUV39", 2021));//Create new Car object
    carList.Add(new Car(5, "dark blue", 4, "HyperF", "Sedan89", 2013));//Create new Car object
    carList.Add(new Car(6, "red", 4, "HyperX", "Truck6", 2020));//Create new Car object
    carList.Add(new Car(7, "yellow", 4, "SuperT", "MiniVan9", 2011));//Create new Car object
    carList.Add(new Car(8, "gray", 4, "SuperF", "Van28", 2022));//Create new Car object
    carList.Add(new Car(9, "white", 4, "SuperRX", "SUV9", 2000));//Create new Car object
    carList.Add(new Car(10, "white", 4, "SuperB", "Sedan12", 2005));//Create new Car object
    return carList;
}

public class Car
{
    public Car(int vinNumber, string color, int doorNumber, string make, string model, int year)
    {
        VinNumber = vinNumber;
        Color = color;
        DoorNumber = doorNumber;
        Make = make;
        Model = model;
        Year = year;
    }
    public Car(int vinNumber)
    {
        VinNumber = vinNumber;//A unique is based only on the Vin number so we need to create an object that just has that imformation 
    }

    public int VinNumber { get; set; }
    public string Color { get; set; }
    public int DoorNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

    public override bool Equals(object? obj)
    {
        return this.VinNumber == ((Car)obj).VinNumber;//This condition needs to provide a unique object 
    }

    public override int GetHashCode()
    {
        return VinNumber.GetHashCode();//Hashcode needs to applyed to each properties which will provide a unique object
    }

    public override string? ToString()//override ToString so that Console.Writeline display below instead of the default
    {
        return $"VinNumber:{VinNumber}, Color:{Color}, DoorNumber:{DoorNumber}, Make:{Make}, Model:{Model}, Year:{Year}";
    }
}
Code Output

VinNumber:5, Color:dark blue, DoorNumber:4, Make:HyperF, Model:Sedan89, Year:2013

List IndexOf Method Speed Test

Next is the speed test to see how IndexOf performs compared to the other methods. This is the same test as before.

List IndexOf Method Speed Test Code
using System.Diagnostics;
int numberOfTests = 10;//Number of tests 
List<double> testSpeedList = new List<double>();

for (int i = 0; i < numberOfTests; i++)
{
    testSpeedList.Add(IndexOfMethodSpeedTest());
}
Console.WriteLine($"IndexOf Method Average speed:{Math.Round(testSpeedList.Average())}ms, In {numberOfTests} tests");

double IndexOfMethodSpeedTest()
{
    List<Car> carList = GetInitialCarList();//Get intial random generated list
    int numberOfFunctionCalls = 50000;//Number of function calls made
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();//Start the Stopwatch timer
    Car selectedCar = null;
    for (int i = 0; i < numberOfFunctionCalls; i++)
    {
        stopwatch.Stop();
        int selectedVinNumber = GetIdToSearchFor(carList);//Not counting this in the time
        stopwatch.Start();
        selectedCar = FindInListByIndexOf(selectedVinNumber, carList);
    }
    stopwatch.Stop();//Stop the Stopwatch timer
    Console.WriteLine($"Function calls:{numberOfFunctionCalls}, In {stopwatch.Elapsed.Minutes}m {stopwatch.Elapsed.Seconds}s {stopwatch.Elapsed.Milliseconds}ms");
    return stopwatch.Elapsed.TotalMilliseconds;
}
Supporting Code
List<Car> GetInitialCarList()
{
    List<Car> carList = new List<Car>();//Create new Car List as empty
    int numberOfObjectsToCreate = 100000;
    int maxNumberOfVinNumber = 999999999;
    int maxNumberOfColorCharacters = 4;
    int maxNumberofWheels = 4;
    int maxNumberOfMakeCharacters = 10;
    int maxNumberOfModelCharacters = 10;
    int maxNumberOfYear = 4;
    for (int i = 0; i < numberOfObjectsToCreate; i++)
    {
        carList.Add(new Car(GetRandomInt(maxNumberOfVinNumber), GetRandomText(maxNumberOfColorCharacters), GetRandomInt(maxNumberofWheels), GetRandomText(maxNumberOfMakeCharacters), GetRandomText(maxNumberOfModelCharacters), GetRandomInt(maxNumberOfYear)));//Add a new Car to the list
    }
    return carList;
}

int GetIdToSearchFor(List<Car> carList)
{
    int randomIndex = GetRandomInt(carList.Count - 1);//Get random index in the car list
    return carList[randomIndex].VinNumber;//return vin number of the random car picked
}

Car FindInListByIndexOf(int vinNumber, List<Car> carList)
{
    Car car = new Car(vinNumber);
    int foundCarIndex = carList.IndexOf(car);
    return carList[foundCarIndex];
}

string GetRandomText(int numberOfCharacters)
{
    string finalOutput = "";
    string alphbet = "abcedfghijklmnopqrstuvwxyz";
    for (int i = 0; i < numberOfCharacters; i++)
    {
        int randomIndex = GetRandomInt(alphbet.Length - 1);//Get random number between 0 and the string's length - 1
        finalOutput += alphbet[randomIndex];//Pick an index based on the random index
    }
    return finalOutput;

}

int GetRandomInt(int maxNumber)
{
    Random random = new Random();//Create Random class
    int randomInt = random.Next(maxNumber);//Get a random number between 0 and the maxnumber
    return randomInt;
}

public class Car
{
    public Car(int vinNumber, string color, int doorNumber, string make, string model, int year)
    {
        VinNumber = vinNumber;
        Color = color;
        DoorNumber = doorNumber;
        Make = make;
        Model = model;
        Year = year;
    }
    public Car(int vinNumber)
    {
        VinNumber = vinNumber;//A unique is based only on the Vin number so we need to create an object that just has that imformation 
    }

    public int VinNumber { get; set; }
    public string Color { get; set; }
    public int DoorNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

    public override bool Equals(object? obj)
    {
        return this.VinNumber == ((Car)obj).VinNumber;//This condition needs to provide a unique object 
    }

    public override int GetHashCode()
    {
        return VinNumber.GetHashCode();//Hashcode needs to applied to each properties which will provide a unique object
    }

    public override string? ToString()//override ToString so that Console.Writeline display below instead of the default
    {
        return $"VinNumber:{VinNumber}, Color:{Color}, DoorNumber:{DoorNumber}, Make:{Make}, Model:{Model}, Year:{Year}";
    }
}
Code Output
Function calls:50000, In 0m 17s 971ms
Function calls:50000, In 0m 18s 41ms
Function calls:50000, In 0m 18s 264ms
Function calls:50000, In 0m 18s 258ms
Function calls:50000, In 0m 18s 290ms
Function calls:50000, In 0m 18s 19ms
Function calls:50000, In 0m 18s 59ms
Function calls:50000, In 0m 17s 979ms
Function calls:50000, In 0m 17s 788ms
Function calls:50000, In 0m 17s 998ms
IndexOf Method Average speed:18067ms, In 10 tests

With an average speed of 18 seconds, that would make this the slowest method so far and even slower than just looping through the items and finding a match.

How Readable Is The List IndexOf Method

While the initial method of reading of finding the index and then using that index to get the object from the list is readable. The underlying mechanics of the equals and get hash code could be easily missed or miss understood while at the same time, there's a need to modify the car object so that only one property can be populated so that the search can for the index be made. The step of creating an object to get an object seems redundant.

How Concise Is The List IndexOf Method

While the method is only two lines and is concise the underlying setup of the object may complicate things if there's a better method.

List FindAll Method

Find all provide a method that returns multiple objects that match, but we can use it to return one object that matches and assume that our condition will only return one match. Let's look at an example.

List FindAll Code Example
List<Car> carList = GetInitialCarList();//Get initial hardcoded list
int vinNumberTofind = 5;//VIN selected by a user to find.

Car car = FindInListByFindAll(vinNumberTofind, carList);//Find the car object by vinNumber through a loop method
Console.WriteLine(car);//Write to screen

Car FindInListByFindAll(int vinNumber, List<Car> carList)
{
    List<Car> cars = carList.FindAll(x => x.VinNumber == vinNumber);//Pass condition to find the object and return it. A null can be returned
    if(cars != null && cars.Count > 0)
    {
        return cars[0];
    }
    return null;
}
Supporting Code
List<Car> GetInitialCarList()
{
    List<Car> carList = new List<Car>();//Create new Car List as empty
    carList.Add(new Car(1, "black", 4, "SuperT", "SUV39", 2020));//Create new Car object
    carList.Add(new Car(2, "yellow", 4, "SuperRX", "Compact4", 2010));//Create new Car object
    carList.Add(new Car(3, "red", 4, "SuperH", "Truck3", 2011));//Create new Car object
    carList.Add(new Car(4, "blue", 4, "HyperT", "SUV39", 2021));//Create new Car object
    carList.Add(new Car(5, "dark blue", 4, "HyperF", "Sedan89", 2013));//Create new Car object
    carList.Add(new Car(6, "red", 4, "HyperX", "Truck6", 2020));//Create new Car object
    carList.Add(new Car(7, "yellow", 4, "SuperT", "MiniVan9", 2011));//Create new Car object
    carList.Add(new Car(8, "gray", 4, "SuperF", "Van28", 2022));//Create new Car object
    carList.Add(new Car(9, "white", 4, "SuperRX", "SUV9", 2000));//Create new Car object
    carList.Add(new Car(10, "white", 4, "SuperB", "Sedan12", 2005));//Create new Car object
    return carList;
}

public class Car
{
    public Car(int vinNumber, string color, int doorNumber, string make, string model, int year)
    {
        VinNumber = vinNumber;
        Color = color;
        DoorNumber = doorNumber;
        Make = make;
        Model = model;
        Year = year;
    }

    public int VinNumber { get; set; }
    public string Color { get; set; }
    public int DoorNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

    public override string? ToString()//override ToString so that Console.Writeline display below instead of the default
    {
        return $"VinNumber:{VinNumber}, Color:{Color}, DoorNumber:{DoorNumber}, Make:{Make}, Model:{Model}, Year:{Year}";
    }
}
Code Output

VinNumber:5, Color:dark blue, DoorNumber:4, Make:HyperF, Model:Sedan89, Year:2013

This gives us what we want. Even though method could objects but by restricting it to a unique identifier we can only one item back.

List FindAll Speed Test

Now for the speed test of this method. The difference between this method and List Find would be that FindAll would need to search the entire list because it looks for duplicates while Find will stop on the first found value. We would expect FindAll to perform slower than Find. Let's look at the test.

List FindAll Speed Test Code
using System.Diagnostics;
int numberOfTests = 10;//Number of tests 
List<double> testSpeedList = new List<double>();

for (int i = 0; i < numberOfTests; i++)
{
    testSpeedList.Add(FindAllMethodSpeedTest());
}
Console.WriteLine($"FindAll Method Average speed:{Math.Round(testSpeedList.Average())}ms, In {numberOfTests} tests");

double FindAllMethodSpeedTest()
{
    List<Car> carList = GetInitialCarList();//Get intial random generated list
    int numberOfFunctionCalls = 50000;//Number of function calls made
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();//Start the Stopwatch timer
    Car selectedCar = null;
    for (int i = 0; i < numberOfFunctionCalls; i++)
    {
        stopwatch.Stop();
        int selectedVinNumber = GetIdToSearchFor(carList);//Not counting this in the time
        stopwatch.Start();
        selectedCar = FindInListByFindAll(selectedVinNumber, carList);
    }
    stopwatch.Stop();//Stop the Stopwatch timer
    Console.WriteLine($"Function calls:{numberOfFunctionCalls}, In {stopwatch.Elapsed.Minutes}m {stopwatch.Elapsed.Seconds}s {stopwatch.Elapsed.Milliseconds}ms");
    return stopwatch.Elapsed.TotalMilliseconds;
}
Supporting Code
List<Car> GetInitialCarList()
{
    List<Car> carList = new List<Car>();//Create new Car List as empty
    int numberOfObjectsToCreate = 100000;
    int maxNumberOfVinNumber = 999999999;
    int maxNumberOfColorCharacters = 4;
    int maxNumberofWheels = 4;
    int maxNumberOfMakeCharacters = 10;
    int maxNumberOfModelCharacters = 10;
    int maxNumberOfYear = 4;
    for (int i = 0; i < numberOfObjectsToCreate; i++)
    {
        carList.Add(new Car(GetRandomInt(maxNumberOfVinNumber), GetRandomText(maxNumberOfColorCharacters), GetRandomInt(maxNumberofWheels), GetRandomText(maxNumberOfMakeCharacters), GetRandomText(maxNumberOfModelCharacters), GetRandomInt(maxNumberOfYear)));//Add a new Car to the list
    }
    return carList;
}

int GetIdToSearchFor(List<Car> carList)
{
    int randomIndex = GetRandomInt(carList.Count - 1);//Get random index in the car list
    return carList[randomIndex].VinNumber;//return vin number of the random car picked
}

Car FindInListByFindAll(int vinNumber, List<Car> carList)
{
    List<Car> cars = carList.FindAll(x => x.VinNumber == vinNumber);//Pass condition to find the object and return it. A null can be returned
    if (cars != null && cars.Count > 0)
    {
        return cars[0];
    }
    return null;
}

string GetRandomText(int numberOfCharacters)
{
    string finalOutput = "";
    string alphbet = "abcedfghijklmnopqrstuvwxyz";
    for (int i = 0; i < numberOfCharacters; i++)
    {
        int randomIndex = GetRandomInt(alphbet.Length - 1);//Get random number between 0 and the string's length - 1
        finalOutput += alphbet[randomIndex];//Pick an index based on the random index
    }
    return finalOutput;

}

int GetRandomInt(int maxNumber)
{
    Random random = new Random();//Create Random class
    int randomInt = random.Next(maxNumber);//Get a random number between 0 and the maxnumber
    return randomInt;
}

public class Car
{
    public Car(int vinNumber, string color, int doorNumber, string make, string model, int year)
    {
        VinNumber = vinNumber;
        Color = color;
        DoorNumber = doorNumber;
        Make = make;
        Model = model;
        Year = year;
    }

    public int VinNumber { get; set; }
    public string Color { get; set; }
    public int DoorNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

    public override string? ToString()//override ToString so that Console.Writeline display below instead of the default
    {
        return $"VinNumber:{VinNumber}, Color:{Color}, DoorNumber:{DoorNumber}, Make:{Make}, Model:{Model}, Year:{Year}";
    }
}
Code Output
Function calls:50000, In 0m 22s 936ms
Function calls:50000, In 0m 22s 938ms
Function calls:50000, In 0m 22s 970ms
Function calls:50000, In 0m 23s 22ms
Function calls:50000, In 0m 23s 601ms
Function calls:50000, In 0m 23s 565ms
Function calls:50000, In 0m 23s 137ms
Function calls:50000, In 0m 23s 193ms
Function calls:50000, In 0m 22s 802ms
Function calls:50000, In 0m 23s 133ms
FindAll Method Average speed:23130ms, In 10 tests

With an average speed of about 24 seconds, this makes FindAll the slowest method so far. It was expected to be slower than Find so that assumption was true. Unless we need to find more than object with the conditional statement then it would be best to avoid this method when finding a single object match

How Readable Is The List FindAll Method

It reads well and returns all matches on the conditional statement and it returns a list of all the matches values and the name implies. The setup is similar to FInd as it takes in the same parameters but the difference is that it returns a list instead of a single object.

How Concise Is The List FindAll Method

It is concise but has more lines of code if we just want to return a single object and we need to do null checks and see if the list has at least one element in it.

Enumerable Where Method

Enumerable Where uses a conditional statement to return all objects that match the condition. It is similar to FindAll so let's look at an example.

Enumerable Where Example Code
List<Car> carList = GetInitialCarList();//Get initial hardcoded list
int vinNumberTofind = 5;//VIN selected by a user to find.

Car car = FindInListByEnumerableWhere(vinNumberTofind, carList);//Find the car object by vinNumber through a loop method
Console.WriteLine(car);//Write to screen

Car FindInListByEnumerableWhere(int vinNumber, List<Car> carList)
{
    IEnumerable<Car> cars = carList.Where(x => x.VinNumber == vinNumber);//Pass condition to find the object and return it. A null can be returned
    if (cars != null && cars.Count() > 0)
    {
        return cars.ElementAt(0);//return the first element in the list, assumed to be only one
    }
    return null;
}
Supporting Code
List<Car> GetInitialCarList()
{
    List<Car> carList = new List<Car>();//Create new Car List as empty
    carList.Add(new Car(1, "black", 4, "SuperT", "SUV39", 2020));//Create new Car object
    carList.Add(new Car(2, "yellow", 4, "SuperRX", "Compact4", 2010));//Create new Car object
    carList.Add(new Car(3, "red", 4, "SuperH", "Truck3", 2011));//Create new Car object
    carList.Add(new Car(4, "blue", 4, "HyperT", "SUV39", 2021));//Create new Car object
    carList.Add(new Car(5, "dark blue", 4, "HyperF", "Sedan89", 2013));//Create new Car object
    carList.Add(new Car(6, "red", 4, "HyperX", "Truck6", 2020));//Create new Car object
    carList.Add(new Car(7, "yellow", 4, "SuperT", "MiniVan9", 2011));//Create new Car object
    carList.Add(new Car(8, "gray", 4, "SuperF", "Van28", 2022));//Create new Car object
    carList.Add(new Car(9, "white", 4, "SuperRX", "SUV9", 2000));//Create new Car object
    carList.Add(new Car(10, "white", 4, "SuperB", "Sedan12", 2005));//Create new Car object
    return carList;
}

public class Car
{
    public Car(int vinNumber, string color, int doorNumber, string make, string model, int year)
    {
        VinNumber = vinNumber;
        Color = color;
        DoorNumber = doorNumber;
        Make = make;
        Model = model;
        Year = year;
    }

    public int VinNumber { get; set; }
    public string Color { get; set; }
    public int DoorNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

    public override string? ToString()//override ToString so that Console.Writeline display below instead of the default
    {
        return $"VinNumber:{VinNumber}, Color:{Color}, DoorNumber:{DoorNumber}, Make:{Make}, Model:{Model}, Year:{Year}";
    }
}
Code Output
VinNumber:5, Color:dark blue, DoorNumber:4, Make:HyperF, Model:Sedan89, Year:2013

This works as intended. If you look at the syntax there are subtle differences in how to get the element and type returns is not a list in this case but of IEnumerable of Car types.

Enumerable Where Speed Test

This is the last method we have to test the performance with since this method is similar to FindAll that which returns a collection of objects that matches the provided condition. We would also expect this method to perform poorly. This is because to ensure that all matches are found the entire list would need to be traversed even if there is only one occupancy in the list.

Enumerable Where Speed Test Code
using System.Diagnostics;
int numberOfTests = 10;//Number of tests 
List<double> testSpeedList = new List<double>();

for (int i = 0; i < numberOfTests; i++)
{
    testSpeedList.Add(EnumerableWhereMethodSpeedTest());
}
Console.WriteLine($"Enumerable Where Method Average speed:{Math.Round(testSpeedList.Average())}ms, In {numberOfTests} tests");

double EnumerableWhereMethodSpeedTest()
{
    List<Car> carList = GetInitialCarList();//Get intial random generated list
    int numberOfFunctionCalls = 50000;//Number of function calls made
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();//Start the Stopwatch timer
    Car selectedCar = null;
    for (int i = 0; i < numberOfFunctionCalls; i++)
    {
        stopwatch.Stop();
        int selectedVinNumber = GetIdToSearchFor(carList);//Not counting this in the time
        stopwatch.Start();
        selectedCar = FindInListByEnumerableWhere(selectedVinNumber, carList);
    }
    stopwatch.Stop();//Stop the Stopwatch timer
    Console.WriteLine($"Function calls:{numberOfFunctionCalls}, In {stopwatch.Elapsed.Minutes}m {stopwatch.Elapsed.Seconds}s {stopwatch.Elapsed.Milliseconds}ms");
    return stopwatch.Elapsed.TotalMilliseconds;
}
Supporting Code
List<Car> GetInitialCarList()
{
    List<Car> carList = new List<Car>();//Create new Car List as empty
    int numberOfObjectsToCreate = 100000;
    int maxNumberOfVinNumber = 999999999;
    int maxNumberOfColorCharacters = 4;
    int maxNumberofWheels = 4;
    int maxNumberOfMakeCharacters = 10;
    int maxNumberOfModelCharacters = 10;
    int maxNumberOfYear = 4;
    for (int i = 0; i < numberOfObjectsToCreate; i++)
    {
        carList.Add(new Car(GetRandomInt(maxNumberOfVinNumber), GetRandomText(maxNumberOfColorCharacters), GetRandomInt(maxNumberofWheels), GetRandomText(maxNumberOfMakeCharacters), GetRandomText(maxNumberOfModelCharacters), GetRandomInt(maxNumberOfYear)));//Add a new Car to the list
    }
    return carList;
}

int GetIdToSearchFor(List<Car> carList)
{
    int randomIndex = GetRandomInt(carList.Count - 1);//Get random index in the car list
    return carList[randomIndex].VinNumber;//return vin number of the random car picked
}

Car FindInListByEnumerableWhere(int vinNumber, List<Car> carList)
{
    IEnumerable<Car> cars = carList.Where(x => x.VinNumber == vinNumber);//Pass condition to find the object and return it. A null can be returned
    if (cars != null && cars.Count() > 0)
    {
        return cars.ElementAt(0);//return the first element in the list, assumed to be only one
    }
    return null;
}

string GetRandomText(int numberOfCharacters)
{
    string finalOutput = "";
    string alphbet = "abcedfghijklmnopqrstuvwxyz";
    for (int i = 0; i < numberOfCharacters; i++)
    {
        int randomIndex = GetRandomInt(alphbet.Length - 1);//Get random number between 0 and the string's length - 1
        finalOutput += alphbet[randomIndex];//Pick an index based on the random index
    }
    return finalOutput;

}

int GetRandomInt(int maxNumber)
{
    Random random = new Random();//Create Random class
    int randomInt = random.Next(maxNumber);//Get a random number between 0 and the maxnumber
    return randomInt;
}

public class Car
{
    public Car(int vinNumber, string color, int doorNumber, string make, string model, int year)
    {
        VinNumber = vinNumber;
        Color = color;
        DoorNumber = doorNumber;
        Make = make;
        Model = model;
        Year = year;
    }

    public int VinNumber { get; set; }
    public string Color { get; set; }
    public int DoorNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

    public override string? ToString()//override ToString so that Console.Writeline display below instead of the default
    {
        return $"VinNumber:{VinNumber}, Color:{Color}, DoorNumber:{DoorNumber}, Make:{Make}, Model:{Model}, Year:{Year}";
    }
}
Code Output
Function calls:50000, In 0m 58s 526ms
Function calls:50000, In 0m 59s 498ms
Function calls:50000, In 0m 58s 980ms
Function calls:50000, In 0m 58s 402ms
Function calls:50000, In 0m 58s 943ms
Function calls:50000, In 0m 58s 944ms
Function calls:50000, In 0m 59s 210ms
Function calls:50000, In 0m 58s 477ms
Function calls:50000, In 0m 58s 703ms
Function calls:50000, In 0m 59s 33ms
Enumerable Where Method Average speed:58872ms, In 10 tests

With an average speed of 58 seconds. This method's performance is terrible and it is by far the worst so avoid this if possible.

How Readable Is The Enumerable Where Method

It reads well and returns all matches on the conditional statement and since it doesn't return a list like FindAll you may need to learn the differences between IEnumerable and list to get what you want.

How Concise Is The Enumerable Where Method

It is concise but has the same lines of code as the FindAll Method.

Alternate Search Method In Dictionary

While this is not a list method but a completely different data structure. It's mentioned here because its search method is superior to that of a list. That is one of the more important distinctions between a list and a dictionary In fact if you have a more search-dependent use case dictionary is typically a better choice to hold the data than a list. The great aspect of dictionaries that makes them great for searches is that the keys that are mapped to values are non-linear and don't have to follow 0 to list.Count - 1 indexing. The keys can be any data type. Let's look at an example.

Alternate Search Method In Dictionary Example Code
Dictionary<int, Car> carDictionary = GetInitialCarDictionary();//Get initial hardcoded list
int vinNumberTofind = 5;//VIN selected by a user to find.

Car car = FindInDictionary(vinNumberTofind, carDictionary);//Find the car object by vinNumber through a loop method
Console.WriteLine(car);//Write to screen

Car FindInDictionary(int vinNumber, Dictionary<int, Car> carDictionary)
{
    if (carDictionary.TryGetValue(vinNumber, out Car car))
    {
        return car;
    }
    return null;
}

Dictionary<int, Car> GetInitialCarDictionary()
{
    Dictionary<int, Car> carDictionary = new Dictionary<int, Car>();//Create new Car Dictionary as empty
    Car car1 = new Car(1, "black", 4, "SuperT", "SUV39", 2020);//Create new Car object
    Car car2 = new Car(2, "yellow", 4, "SuperRX", "Compact4", 2010);//Create new Car object
    Car car3 = new Car(3, "red", 4, "SuperH", "Truck3", 2011);//Create new Car object
    Car car4 = new Car(4, "blue", 4, "HyperT", "SUV39", 2021);//Create new Car object
    Car car5 = new Car(5, "dark blue", 4, "HyperF", "Sedan89", 2013);//Create new Car object
    Car car6 = new Car(6, "red", 4, "HyperX", "Truck6", 2020);//Create new Car object
    Car car7 = new Car(7, "yellow", 4, "SuperT", "MiniVan9", 2011);//Create new Car object
    Car car8 = new Car(8, "gray", 4, "SuperF", "Van28", 2022);//Create new Car object
    Car car9 = new Car(9, "white", 4, "SuperRX", "SUV9", 2000);//Create new Car object
    Car car10 = new Car(10, "white", 4, "SuperB", "Sedan12", 2005);//Create new Car object
    carDictionary.Add(car1.VinNumber, car1);//Add car reference as a key and value
    carDictionary.Add(car2.VinNumber, car2);//Add car reference as a key and value
    carDictionary.Add(car3.VinNumber, car3);//Add car reference as a key and value
    carDictionary.Add(car4.VinNumber, car4);//Add car reference as a key and value
    carDictionary.Add(car5.VinNumber, car5);//Add car reference as a key and value
    carDictionary.Add(car6.VinNumber, car6);//Add car reference as a key and value
    carDictionary.Add(car7.VinNumber, car7);//Add car reference as a key and value
    carDictionary.Add(car8.VinNumber, car8);//Add car reference as a key and value
    carDictionary.Add(car9.VinNumber, car9);//Add car reference as a key and value
    carDictionary.Add(car10.VinNumber, car10);//Add car reference as a key and value
    return carDictionary;
}
Supporting Code
public class Car
{
    public Car(int vinNumber, string color, int doorNumber, string make, string model, int year)
    {
        VinNumber = vinNumber;
        Color = color;
        DoorNumber = doorNumber;
        Make = make;
        Model = model;
        Year = year;
    }

    public int VinNumber { get; set; }
    public string Color { get; set; }
    public int DoorNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

    public override string? ToString()//override ToString so that Console.Writeline display below instead of the default
    {
        return $"VinNumber:{VinNumber}, Color:{Color}, DoorNumber:{DoorNumber}, Make:{Make}, Model:{Model}, Year:{Year}";
    }
}
Code Output
VinNumber:5, Color:dark blue, DoorNumber:4, Make:HyperF, Model:Sedan89, Year:2013

Alternate Search Method In Dictionary Speed Test Doing the same test as in the list we'll see how the dictionary performs.

Alternate Search Method In Dictionary Speed Test Code
using System.Diagnostics;
int numberOfTests = 10;//Number of tests 
List<double> testSpeedList = new List<double>();

for (int i = 0; i < numberOfTests; i++)
{
    testSpeedList.Add(DictionarySearchMethodSpeedTest());
}
Console.WriteLine($"Dictionaary Search Method Average speed:{Math.Round(testSpeedList.Average())}ms, In {numberOfTests} tests");

double DictionarySearchMethodSpeedTest()
{
    Dictionary<int, Car> carDictionary = GetInitialCarDictionary();//Get intial random generated Dictionary
    int numberOfFunctionCalls = 50000;//Number of function calls made
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();//Start the Stopwatch timer
    Car selectedCar = null;
    for (int i = 0; i < numberOfFunctionCalls; i++)
    {
        stopwatch.Stop();
        int selectedVinNumber = GetIdToSearchFor(carDictionary);//Not counting this in the time
        stopwatch.Start();
        selectedCar = FindInDictionary(selectedVinNumber, carDictionary);
    }
    stopwatch.Stop();//Stop the Stopwatch timer
    Console.WriteLine($"Function calls:{numberOfFunctionCalls}, In {stopwatch.Elapsed.Minutes}m {stopwatch.Elapsed.Seconds}s {stopwatch.Elapsed.Milliseconds}ms");
    return stopwatch.Elapsed.TotalMilliseconds;
}

Dictionary<int, Car> GetInitialCarDictionary()
{
    Dictionary<int, Car> carDictionary = new Dictionary<int, Car>();//Create new Car dictionary as empty
    int numberOfObjectsToCreate = 100000;
    int maxNumberOfVinNumber = 999999999;
    int maxNumberOfColorCharacters = 4;
    int maxNumberofWheels = 4;
    int maxNumberOfMakeCharacters = 10;
    int maxNumberOfModelCharacters = 10;
    int maxNumberOfYear = 4;
    for (int i = 0; i < numberOfObjectsToCreate; i++)
    {
        bool isAdded = false;
        while (!isAdded)
        {
            Car car = new Car(GetRandomInt(maxNumberOfVinNumber), GetRandomText(maxNumberOfColorCharacters), GetRandomInt(maxNumberofWheels), GetRandomText(maxNumberOfMakeCharacters), GetRandomText(maxNumberOfModelCharacters), GetRandomInt(maxNumberOfYear));
            if (!carDictionary.ContainsKey(car.VinNumber))
            {
                carDictionary.Add(car.VinNumber, car);//Add a new Car to the dictionary
                isAdded = true;
            }
        }
    }
    return carDictionary;
}

int GetIdToSearchFor(Dictionary<int, Car> carDictionary)
{
    int randomIndex = GetRandomInt(carDictionary.Count - 1);//Get random index in the car Dictionary
    return carDictionary.ElementAt(randomIndex).Value.VinNumber;//Vin number of the random car picked
}

Car FindInDictionary(int vinNumber, Dictionary<int, Car> carDictionary)
{
    if (carDictionary.TryGetValue(vinNumber, out Car car))
    {
        return car;
    }
    return null;
}
Supporting Code
string GetRandomText(int numberOfCharacters)
{
    string finalOutput = "";
    string alphbet = "abcedfghijklmnopqrstuvwxyz";
    for (int i = 0; i < numberOfCharacters; i++)
    {
        int randomIndex = GetRandomInt(alphbet.Length - 1);//Get random number between 0 and the string's length - 1
        finalOutput += alphbet[randomIndex];//Pick an index based on the random index
    }
    return finalOutput;

}

int GetRandomInt(int maxNumber)
{
    Random random = new Random();//Create Random class
    int randomInt = random.Next(maxNumber);//Get a random number between 0 and the maxnumber
    return randomInt;
}

public class Car
{
    public Car(int vinNumber, string color, int doorNumber, string make, string model, int year)
    {
        VinNumber = vinNumber;
        Color = color;
        DoorNumber = doorNumber;
        Make = make;
        Model = model;
        Year = year;
    }

    public int VinNumber { get; set; }
    public string Color { get; set; }
    public int DoorNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

    public override string? ToString()//override ToString so that Console.Writeline display below instead of the default
    {
        return $"VinNumber:{VinNumber}, Color:{Color}, DoorNumber:{DoorNumber}, Make:{Make}, Model:{Model}, Year:{Year}";
    }
}
Code Output
Function calls:50000, In 0m 0s 7ms
Function calls:50000, In 0m 0s 7ms
Function calls:50000, In 0m 0s 7ms
Function calls:50000, In 0m 0s 7ms
Function calls:50000, In 0m 0s 7ms
Function calls:50000, In 0m 0s 7ms
Function calls:50000, In 0m 0s 7ms
Function calls:50000, In 0m 0s 7ms
Function calls:50000, In 0m 0s 7ms
Function calls:50000, In 0m 0s 7ms
Dictionaary Search Method Average speed:7ms, In 10 tests

Yes, that is correct the dictionary can perform the same test as the list in 7 milliseconds.

Conclusion

Overall RankMethodSpeedConciseReadability(1-5)
1Dictionary Collection Type7ms8 lines4
2List Find Method11562ms3 lines4
3Base Case Loop Method14739ms11 lines4
4List IndexOf Method18067ms6 lines3
5List FindAll Method23130ms9 lines3
6Enumerable Where Method58872ms9 lines3

The best method for finding an item in a list is List Find, however, the best collection type for finding an item is in the dictionary. If starting from scratch and searching for items was the main use case then a dictionary is the better choice. But often is the case you may have not designed the system in use and have no choice but to use a list. List Find is the best of the list methods because it is the fastest list method. It's easy to use and set up and also very compact. Next third best method is writing our method to loop through the list and pick the item that we want. It may seem like more work doing it that way but there is flexibility in crafting exactly what we want. The IndexOf Method, FindAll, and Enumerable Where are all too slow to recommend use and should be avoided for this use case of finding an item in the list.

Get Latest Updates
Comments Section