Home > DeveloperSection > Forums > C# iterate on class own elements
Ashish Pandey

Total Post:128

Points:898
Posted on    August-18-2014 3:10 AM

 C# C# 
Ratings:


 1 Reply(s)
 818  View(s)
Rate this:

I have created a class which inherits from List: Now I want to iterate the elements of the class in the class itself and return the object if it satisfies certain criteria

protected class myList : List<Object>

{

    getElement(string name)

    {

        ??? LOOP AND IF CONDITION ???

         return element;

    }

}

Can you help? Thanks



Pravesh Singh

Total Post:411

Points:2881
Posted on    August-18-2014 3:33 AM

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;

    }


Don't want to miss updates? Please click the below button!

Follow MindStick