Bob T.
Bob T.

Reputation: 261

Searching for specific member of List in c#

I have three classes (Street, Shop , StartingPoint) inherited from class Event. I created ListList<Event> eventList = new List<Event>(); and added them in List : eventList.Add(new Street()); eventList.Add(new Shop());etc. How can i acces specific class ? For example i want working only with Shop. How can i acces it ?

Upvotes: 0

Views: 89

Answers (2)

brothers28
brothers28

Reputation: 1206

Like Selman22 said you can use eventList.OfType<Shop>().First(); to access on the first shop in your list. If you want to have a specified shop you have to work with the name of the shop or the street or whatever.

This will then look like;

eventList.OfType<Shop>().FirstOrDefault(x => x.StreetName == "WallStreet");

That returns you the Shop where the streetname of the Shop is like "WallStreet"

Upvotes: 0

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

With indexer ?

eventList[index]

Or LINQ ?

eventList.OfType<Shop>().First();

OfType<Shop>() will return all Shops.If you want single Shop you can use First.

Upvotes: 1

Related Questions