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)
    {
        WriteLine(item.address);
    }

    // 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");
        }
    );
    book.RemoveAt(index);

    // List to verify Boris Johnson was removed from our list
    foreach(var item in book)
    {
        WriteLine(item.address);
    }
}

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.

About Author

Related Posts

C# Reference Types

Understanding C# Reference Types

One of the key features of C# is its support for C# reference types, which allow developers to create complex, object-oriented applications. In this blog post, we…

c# value types

Understanding C# Value Types

C# is a powerful programming language that is widely used for developing a wide range of applications. One of the key features of C# is its support…

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…

Leave a Reply