C# ITERATE ON CLASS OWN ELEMENTS

Ashish Pandey

Total Post:128

Points:898
Posted by  Ashish Pandey
C# 
C#
 1098  View(s)
Ratings:
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

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: C# iterate on class own elements

    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;

        }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!