Coding C# Indexing a List of abstract type via string member in the abstract type

mjeschke 40 Reputation points
2025-05-22T19:37:23.86+00:00

Trying to index a list of type abstract class via a member of the class. I'm sure there's examples of this out there but I cannot find them searching the web. How could I say index the list via the abstract class member string ID?

Thanks!

public class Abstract

{

public string ID;

public int value;

public int tolerance;

...

}

// I create a list

List<Abstract> list = new List<Abstract>();

// Add several instances to the list

Abstract instance = new Abstract();

list.Add(instance);

...

...

// Now I want to index the list via ID number string. How can I do this?

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2025-05-22T19:58:41.85+00:00

    For example:

    Dictionary<string, Abstract> index = list.ToDictionary( a => a.ID );
    

    The ID and objects in the list must be unique.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.