C# return index of a object in a List

How to return the index of a list type that is an object in C#. Most examples only shows simple string types when they show how to find the index in a List. What if you have an List of class objects? Well, lets look at how we can use C# return index of a object in a List.

Create a simple Address Class

We will use a simple Address class to demostrate how to find an specific object in a list.

class AddressBook
    public string name;
    public string address;
    public AddressBook(string name, string address)
        this.name = 
        this.address = address;

A little test program

Let’s create a list, add some names and address. Then we will try to search for the index for some addresses. It is a simple address program to save your friends addresses.

static void Main(string[] args)
    List<AddressBook> book = new List<AddressBook>();

    // Add your friends
    book.Add(new AddressBook("Joe Biden", "White House"));
    book.Add(new AddressBook("Vladimir Putin", "Putins Palace"));
    book.Add(new AddressBook("Boris Johnson", "10 Downing Street"));

    // List their address to verify the list is ok
    foreach(var item in book)

    // Boris Johnsons is not a friend anymore, so we decide to remove him
    int index = book.FindIndex(
        delegate (AddressBook book)
            return book.address.Equals("10 Downing Street");

    // List to verify Boris Johnson was removed from our list
    foreach(var item in book)

This example shows how you find the index of an object based on the property “Address”. You could of course use the “Name” instead. It would make more sense here since we are removing an person from an address book. Read more about delegates on docs page.

Read about install Netcore on Debian.

Related Posts

C# check if server is online

C# check if server is online directly from your code. Check servers or services like web servers, database servers like MySQL and MongoDB. You can probably check…

C# Convert Int to Char

C# convert int to char google search is giving some result which I think is not directly what some people want. Most results are giving instructions on…

c# bash script

C# Bash Script Made Easy

There are many reasons why it could be handy to run a bash script from a C# application. In 2014, before I changed to a Mac as…

go hello world

Golang Hello World, Get a Easy Fantastic Start

Golang hello world example tutorial. I assume you are new to the golang language since you found this website. Go is one of the latest programming languages…

Unity persistant datapath

Unity Persistent Data Path

The Unity persistentDataPath is read only and will return the path where you can save data that will be persitent betwen sessions. Even app updates doesn’t touch…

csharp ping check

C# Ping Check the easy way

How can we check if a computer, server, or another device is online? We can use a C# Ping check. If you can check the device with…

Leave a Reply