---
title: "C# iterate on class own elements"  
description: "C# iterate on class own elements"  
author: "Anonymous User"  
published: 2014-08-18  
updated: 2014-08-18  
canonical: https://www.mindstick.com/forum/2047/c-sharp-iterate-on-class-own-elements  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# C# iterate on class own elements

I have created a [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) which [inherits](https://www.mindstick.com/interview/85/what-is-a-subclass) from List: [Now](https://yourviews.mindstick.com/view/81402/it-s-liberals-vs-progressives-in-us-politics-now) I want to iterate the [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-elements) of the class in the class itself and return the object if it satisfies [certain criteria](https://www.mindstick.com/articles/12944/mattresses-for-kids-and-mothers-should-meet-certain-criteria-issues-you-should-consider)

```
protected class myList : List<Object> {    getElement(string name)    {        ??? LOOP AND IF CONDITION ???         return element;    }}
```

Can you help? Thanks

## Replies

### Reply by Pravesh Singh

Hi Ashish, try this:

```
    public Object GetElement(string name)    {        Object element = null;        foreach (var item in this)        {            if (item.ToString() == name)            {               
           element = item;                break;            }        }        return element;    }
```


---

Original Source: https://www.mindstick.com/forum/2047/c-sharp-iterate-on-class-own-elements

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
